Sign in to follow this  
uncle_rico

[web] Deleting multiple table rows using PHP and MySQL

Recommended Posts

Hello there. I am writing some PHP that will allow someone to quickly view all of the rows in the table, check off the ones they want to delete, and then simply hit the "Delete Rows" button to delete them. I thought it would be easiest just to include both the PHP code that displays the list of rows, as well as the PHP code that deletes the selected rows, in the same PHP document. That way, the form's action can just call on the same PHP document. I'll enclose the PHP code that deletes the selected rows in an if($_POST['submit']) {} so that it is only processed if the user just hit the Submit button. Now, I know how to delete a row in MySQL and I know how to run a MySQL query through PHP using mysql_query(). But, I want a way to find out specifically which rows have been selected by the user prior to hitting the Submit buttons and only delete those rows. If I was using C++, I'd probably just set up a bool array to store the value of each row (TRUE if the user checked the row and FALSE if they left the row unchecked). Then, I'd use a for loop to delete only those rows which have a TRUE value. But, with PHP it doesn't seem to be that easy. I know I could use $_POST['name'] to access each checkbox value, where 'name' is the name I give the checkbox. Only, in this case, I don't know beforehand what each name will be. The name of each checkbox is the value of the ItemID field of the row it corresponds to (a value that is unique for each row). I don't know beforehand how many rows will be displayed nor what their corresponding checkboxes will be called, so I don't know if I can use $_POST to access their values. Perhaps there is a different way to index those values rather than using the checkbox's name? I dunno.

Share this post


Link to post
Share on other sites
For each checkbox, make its name something like "rows[]" - the ending brackets are important. Then in PHP you can do this:


$rows = $_POST['rows'];

foreach ($rows as $row) {
// delete $row
// $row[0] is the value of the first checkbox with name "rows[]"
}



Remember to make the value of each checkbox be the ID of the record or something that uniquely identifies the record so that you know what to delete.

Share this post


Link to post
Share on other sites
You might know this, but you can delete multiple rows in a single query, saving you some round trips to the server:

DELETE FROM foo WHERE ItemID IN (1,2,3)

Deleting all rows with ItemID equal to 1,2 and 3.

So insted of one delete per iteration in the foreach, you would build the string to feed the IN operator and execute the query after the loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
For each checkbox, make its name something like "rows[]" - the ending brackets are important. Then in PHP you can do this:

*** Source Snippet Removed ***

Remember to make the value of each checkbox be the ID of the record or something that uniquely identifies the record so that you know what to delete.


Thanks. I'm not sure if I understand your suggestion 100%. What does the "as" keyword mean? And should I name my checkboxes '$rows['ItemID']' or just 'itemID'?

Share this post


Link to post
Share on other sites
You should name your checkboxes "rows[]" exactly (well, without the quotes). When PHP sees a variable coming from a form whose last two characters are "[]" it will make that an array. Dont put anything between those two brackets. The documentation

And the "as" keyword is used in foreach loops. The documentation

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this