Forum Moderators: coopster
I have a page with a repeat region from a MySQL database. Along side each entry is a checkbox and when submitted it appends all the ID's where a tick box has been checked to the querystring. I have then used the following code to delete the records. It only deletes 1 record (presumably the first). How do I do about scrolling through. Do i need a loop or something.
$sql = "DELETE FROM MMUser WHERE UserID=$ID";
Regards and thanks for any suggestions.
Richard
The best way to do it is by using an array.
so for your form :
echo "<input type='radio' name='f_active[{$row[id]}]' value='X'>";
echo "<input type='radio' name='f_active[{$row[id]}]' value='Z'>";
echo "<input type='hidden' name='oldactive[{$row[id]}]' value='{$row[active]}'";
then pass it to a page that updates the database
foreach ($f_active as $id => $state)
{
if ($state!=$oldactive[$id])
{
password stuff etc
$query="UPDATE databse SET active='$state', posted='$date' WHERE id='$id' ";
mysql_query($query);
mysql_close();
echo "Record Updated<br>";
The Checkboxes on the first page are created via a loop. I have given them all the same name (ID) and when they post to the second page only the last one is deleted.
Any thoughts, this is not my strong point.
Cheers
The code on the second page is:
<?php
include("mysqlconn.php");
?>
<?php
$array = ($ID);
$sql = "DELETE FROM MMUser WHERE UserID IN ($array) ";
$pResult=mysql_query( $sql , $db_connection );
$errMsg = mysql_error();
if( $pResult == false ) {
echo "Error: $errMsg<br />Query: $sql<br />\n";
return;
}
mysql_close($db_connection);
?>