I have been looking for hours on how to do this and haven't found a solution yet, hope someone here can help! Part of my problem is that I'm new to php and web dev, so I don't know what I'm looking for terminology wise.
Goal: This is part of a form where users can record countries they have visited. If they have already visited the country, I update with the most recent date that they were there; if it was a new visit, we add that entry to the database as a new country visit.
Problem: When I wrote this page, I hardcoded an array with variables that I could reference later in my code if the variable existed. The reason it may not be present is because I am dumping a bunch of user selections from a previous page, so the variable $china would exist if a user had checked that box, otherwise it wouldn't. I am getting these values through $_POST by looping through all the values. I know this probably wasn't right, but it got the job done to test a couple of countries.
What I have now:
//Post values from form
foreach (array_keys($_POST) as $key) {
$$key = ($_POST[$key]);
}
//my array of possible countries to visit (date is singular as calendar entry for a year, so same for all entries)
$myarray=array($america,$japan,$china)
//check for record for insert or update
foreach ($myarray as $key) {
if (empty($key)) {
continue;
}
$qry = "select * from countries_visited where country='$key'";
$results = mysql_query($qry);
//If record, update. Otherwise, insert new
if (mysql_num_rows($results) > 0) { mysql_query("UPDATE countries_visited set date='$date', timestamp=NOW() where user_id ='{$_SESSION['SESS_MEMBER_ID']} and country ='$key'");
} else {
mysql_query("INSERT INTO countries_visited (date, user_id, country) VALUES ('$date', '{$_SESSION['SESS_MEMBER_ID']}','$key')");
}
}
In a nutshell, I want the $myarray to be populated the list from my basic countries table:
$query="select concat('$',country) as country from countries";
$result=mysql_query($query);
while($row = mysql_fetch_array($result))
{
$array[] = $row;
}
But this isn't working for me. From what I can tell, this is returning a multidimensional array and I'm not handling it correctly. I just want it to store in a basic array like I hardcoded! I concatednated the '$' to make it appear as a variable, as that is how I had it before. Any help is much appreciated