Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP multiple delete with check box

     
3:06 pm on Jun 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


Hello

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

3:15 pm on June 26, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 8, 2001
posts:690
votes: 0


how are you creating the checkboxes?

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>";

11:08 am on July 1, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


Thanks for the response Knighty. I am still having difficulties though.

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);
?>

8:12 pm on July 1, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
posts:475
votes: 0


When you create multiple-select or checkbox elements which are to be processed by PHP, the name you give them must end in a pair of square brackets:

<input type="checkbox" name="check[]" />

That enables the various elements to be accessed as an array (in this case $check[0], $check[1] etc).

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members