This small foreach loop is giving me trouble, see a problem?
Posted: Sat May 29, 2004 5:39 pm
Hi all..
After finally resolving one problem, I ran into another that is so bugging me. This loop will not run the update query everytime through it, only at the end of the loop. Can anyone please tell me why?
So the foreach loop above takes all the "variable variables" form inputs and processes them. This is what the input looks like.. populated in a while loop.
Now if I put a ( print "$link_id has the value $link_order"; ) above or below the UPDATE query, it prints just fine. But in the UPDATE query, using the same variables, it only updates the last one in the loop.
If anyone can help with this I would greatly appreciate it!
Thank you
After finally resolving one problem, I ran into another that is so bugging me. This loop will not run the update query everytime through it, only at the end of the loop. Can anyone please tell me why?
Code: Select all
<?
if($_POST)
{
mysql_connect("$db_host", "$db_user", "$db_pass") or die ("DataBase connection error! Please try again later");
foreach (array_keys($_POST) as $key)
{
$$key = $_POST[$key];
$link_id = $key; ///<- printing this
$link_order = ${$key}; ///<- and this both work in every iteration
// This query won't update on every loop, just last iteration
$query="UPDATE menu SET link_order='$link_order' WHERE link_id='$link_id'";
}
mysql_db_query($db_db,$query);
}
?>Code: Select all
<input type="text" name="<?=$link_id?>" value="<?=$link_order?>" maxlength="3" size="3">If anyone can help with this I would greatly appreciate it!
Thank you