supanoob Posted July 8, 2007 Share Posted July 8, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/ Share on other sites More sharing options...
Wuhtzu Posted July 8, 2007 Share Posted July 8, 2007 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" Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/#findComment-292477 Share on other sites More sharing options...
supanoob Posted July 8, 2007 Author Share Posted July 8, 2007 Thanks best reply to a post ive made, very detailed. Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/#findComment-292503 Share on other sites More sharing options...
sasa Posted July 8, 2007 Share Posted July 8, 2007 Query the database once: "DELETE FROM table WHERE id=$id_to_delete_1 AND id=$id_delete_2 AND id=$id_delete_3" SQL must be "DELETE FROM table WHERE id=$id_to_delete_1 OR id=$id_delete_2 OR id=$id_delete_3" Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/#findComment-292554 Share on other sites More sharing options...
Wuhtzu Posted July 8, 2007 Share Posted July 8, 2007 Thanks sasa, my mistake Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/#findComment-292560 Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/58938-solved-deleting-using-checkboxes/#findComment-292603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.