RRT Posted September 11, 2012 Share Posted September 11, 2012 I am looking for help trying to select multiple checkboxes to delete entries from a DB via a php web page. I know the SQL syntax on *how* to delete a row of data within a SQL database, just not how to create a php array where I can do multiple delete commands at once. For example, if I have a PostgreSQL database with the following rows: database1=# SELECT * FROM table1; cid | home_location | asking_price ----+---------------------+----------------- 1 123 Main Street 200000 2 345 First Ave 210000 3 456 Frontage Rd 199900 4 678 5th Street 205000 5 1001 Elm Street 225000 I would like to display the results of this SELECT ALL query onto a php web page with checkboxes next to each row, giving me the ability to check multiple rows and then delete them from the database. The php code to display the selections looks something like this: <?php // Connecting to the database server $dbconnect = pg_connect("host=aaa.bbb.ccc.ddd dbname=database1 user=user1 password=password1") or die("Could not connect: " . pg_last_error()); //Performing the SQL query $removequery = sprintf("DELETE FROM table1 WHERE CID="?"); How would you suggest that i put all the desired queries together to be able to delete various ones at the same time? I assume that I need to create some sort of array and then be able to delete them all at once, just not sure of how to go about coding that to work. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 11, 2012 Share Posted September 11, 2012 You need to implode () all of the IDs, to a comma-separated list, then use WHERE `field` IN() in the SQL query to select them all. Quote Link to comment Share on other sites More sharing options...
blacknight Posted September 11, 2012 Share Posted September 11, 2012 im gona make your life way easier..... basic check box array usage <input type="checkbox" name="delete[]" id="id#" value='id#' /> id# being your cid number then you pass it using a form post heres the magic in php if (isset($_POST['delete'])) { foreach ($_POST['delete'] as $c => $id) { $d = "DELETE FROM `yourtablename` WHERE `cid` = '".$id."';"; } } and you can now delete many boxes at the same time ! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 11, 2012 Share Posted September 11, 2012 blacknight: The HTML example you posted is correct, but the PHP/MySQL code is wasteful and not recommended. Use the method outlined in my post instead, as it requires only one query no matter how many rows you want to delete. Not to mention that you should not use quotation marks around numerical values in a query, and that you're missing both input validation and output escaping in your example. At the very least typecast the IDs to int, as it'll prevent attacks. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 11, 2012 Share Posted September 11, 2012 I agree with Christian. There's an example here http://forums.phpfreaks.com/index.php?topic=364908.msg1729389#msg1729389 Quote Link to comment 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.