MySQL Forums
Forum List  »  PHP

php foreach and mysql update for an array from a form
Posted by: S
Date: February 07, 2006 02:38PM

Hi All,

I'm having major frustrations updating a table using an array generated from a form. The array posts fine, the sql query is generated fine in the foreach statement, but only one changed row in the array will update, no matter how I combine them. Here's the code:


print_r(array_values($Approval));
echo "<br>";
foreach($Approval as $field=>$data)
{
echo "ID: ".$data[ID]." Approve: ".$data[accept]."<br>";
mysql_connect("$Server","$DB","$Password");
mysql_select_db("$DB");
$updatestring="UPDATE comments SET Approved='$data[accept]' WHERE timestamp='$data[ID]' LIMIT 1;";
$update=mysql_query($updatestring);
if($update)
{
echo "update successful.<br>";
echo $updatestring."<br>";
}
else
{
echo "update NOT successful.<br>";
echo $updatestring."<br>";
}
}

Here's the result:

Array ( [0] => Array ( [accept] => yes [ID] => 20060207125145 ) [1] => Array ( [accept] => yes [ID] => 20060207124426 ) [2] => Array ( [accept] => yes [ID] => 20060207124327 ) )

ID: 20060207125145 Approve: yes
update successful.
UPDATE comments SET Approved='yes' WHERE timestamp='20060207125145' LIMIT 1;

ID: 20060207124426 Approve: yes
update NOT successful.
UPDATE comments SET Approved='yes' WHERE timestamp='20060207124426' LIMIT 1;

ID: 20060207124327 Approve: yes
update NOT successful.
UPDATE comments SET Approved='yes' WHERE timestamp='20060207124327' LIMIT 1;

Update Complete!


So you can see, the first update is working just fine. Subsequent updates are not. If the update of the first query does not change the field "Approved", then the second query is successful but not the third. Is this a problem with MYSQL, with PHP, or with my coding? Its running on a linux installation with PHP Version 4.3.11 and it looks like MYSQL Client API version 3.23.49

Thanks in Advance.
---S



Edited 1 time(s). Last edit at 02/07/2006 02:41PM by S .

Options: ReplyQuote


Subject
Written By
Posted
php foreach and mysql update for an array from a form
S
February 07, 2006 02:38PM
S
February 08, 2006 07:00PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.