Jump to content

Recommended Posts

Ok, so im wanting to delete multiple things from a database using checkboxes, now i know how to use checkboxes in php etc. my question is would this be done using a basic while loop? i mean would i query all the things and then do

 

while ($check_box == 'ON')

{

do the delete script

}

 

or is there something more to it?

Link to comment
https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/
Share on other sites

There is quite a few approaches which will lead to the functionality you want. All of them have to end up with one of the two following "SQL queries":

 

Query the database once:

"DELETE FROM table WHERE id=$id_to_delete_1 AND id=$id_delete_2 AND id=$id_delete_3"

 

Query the database multiple times:

foreach($ids_to_delete as $id_to_delete) {
    mysql_query("DELETE FROM table WHERE id=$id_to_delete");
}

 

How you get to one of those final "statements" may depend on the given project / situation but it will be something like:

 

<!-- This form is of course created by looping through some database results -->
<!-- The <input>-fields are named as an array which will result in $_POST['ids_to_delete'] being an array holding all the ID's we want to delete -->
<form action="" method="post">
  Post 5 <input type="checkbox" name="ids_to_delete[]" value="5">
  Post 6 <input type="checkbox" name="ids_to_delete[]" value="6">
  Post 19  <input type="checkbox" name="ids_to_delete[]" value="19">
  Post 31  <input type="checkbox" name="ids_to_delete[]" value="31">
</form>

<?php
$ids_to_delete = $_POST['ids_to_delete'];

//Run the delete-query for each entry in the $ids_to_delete array
foreach($ids_to_delete as $id_to_delete) {
  $result = mysql_query("DELETE FROM table WHERE id=$id_to_delete");
  
  if($result) {
    echo "Post $id_to_delete was successfully deleted";
  }
  else {
    echo "Post $id_to_delete was not deleted";
  }

}

?>

 

I don't have any really good reason for using multiple queries instead of one or the other way round. I guess there is no doubt that the single query method is fastest, but since you wont be deleting 1000 posts I wont be concerned about speed / performance. Personally I would stick with the multiple query method since it allows you to track the progress / status of each deletion very easily and you do not have to make some ugly code to brew up the "WHERE id=5 AND id=6 AND id=19 and ID=31" :)

I prefer the more efficient single query approach.

 

Give each c/box a value of the record id.

 

<?php
if (isset($_GET['del_id']))
{
    $del_id_list = join (',', $_GET['del_id']);
    
    $sql = "DELETE FROM tablename WHERE id IN ($del_id_list)";
    
    echo $sql;             // execute delete query here
}

?>
<form>
item 1 <input type="checkbox" name="del_id[]" value="1" /> <br />
item 2 <input type="checkbox" name="del_id[]" value="2" /> <br />
item 3 <input type="checkbox" name="del_id[]" value="3" /> <br />
item 4 <input type="checkbox" name="del_id[]" value="4" /> <br />
item 5 <input type="checkbox" name="del_id[]" value="5" /> <br />
item 6 <input type="checkbox" name="del_id[]" value="6" /> <br />
<input type="submit" name="sub" value="Delete selected">
</form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.