Forum Moderators: coopster
I generate a form using a database select statement so I get rows with checkboxes generated in a while loop like...
<input type='checkbox' name='id[]' value='".$row['id']."'>
and then for each checkbox which is checked I want to update the database for that item, like...
$query = ("UPDATE database SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='---ARRAY CLEVERNESS GOES HERE---'");
but I can't get it to work. I can get it to work with the last item that is checked but not for multiple checked items.
Any help would be appreciated.
Cheers
and then for each checkbox which is checked I want to update the database for that item, like...
I can get it to work with the last item that is checked but not for multiple checked items.
That is just because you the variable contains the last assigned value or whatever :)
Any help would be appreciated.
Hope that helped.
Habtom
After getting word that my table needed one more field of a similar nature I duplicated the code, so now there are two rows of checkboxes for each row, controlled by one update button. The SQL queries are now like:
$id_req = $_REQUEST['id'];
foreach($id_req as $id_rows)
{
$query = ("UPDATE hhiporders SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='".$id_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
$arch_req = $_REQUEST['arch'];
foreach($arch_req as $arch_rows)
{
$query = ("UPDATE hhiporders SET archived = '".date("d-m-Y")."' WHERE id='".$arch_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
Now the funny thing is that the code works fine and does the necessary database updates, but it still returns
Warning: Invalid argument supplied for foreach() in /...printed.php on line 36
which is the first of the two foreach statements. So why's it complaining? It works fine, I just want it not to send this error message back to me...
Any ideas?
One of the sets of checkboxes will be empty, so the array will be empty. You can`t access an empty array using a foreach loop. You need to test the array has data before you access it:
$id_req = $_REQUEST['id'];
if (!empty($id_req))
{
foreach($id_req as $id_rows)
{
$query = ("UPDATE hhiporders SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='".$id_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
}
$arch_req = $_REQUEST['arch'];
if (!empty($arch_req))
foreach($arch_req as $arch_rows)
{
$query = ("UPDATE hhiporders SET archived = '".date("d-m-Y")."' WHERE id='".$arch_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
}
dc