homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP multiple delete with check box
ukgimp




msg:1308441
 3:06 pm on Jun 26, 2002 (gmt 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

 

knighty




msg:1308442
 3:15 pm on Jun 26, 2002 (gmt 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>";

ukgimp




msg:1308443
 11:08 am on Jul 1, 2002 (gmt 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);
?>

rewboss




msg:1308444
 8:12 pm on Jul 1, 2002 (gmt 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).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved