407 posts
  • Has been part of the Envato Community for over 5 years
  • Located in Australia
envato says

Sometimes, your users might want to delete more than one record at a time; this tutorial will show you how to do so. Using PHP and a MySQL database, I’ll take you step-by-step and even throw in a simple zebra striping technique using jQuery.

Click here to see full post: http://blog.themeforest.net/tutorials/deleting-multiple-records-with-php/

1 post
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in United States
mikesir87 says

As this post may be viewed by developers new to programming, I want to note that you will most certainly want to sanitize any data coming in, as it is being used directly. It’s probably best to use prepared statements. This will prevent someone from possibly deleting all rows in your table, or even worse. For those who want more info, check out http://us.php.net/mysqli, and then look at the prepared queries section.

Otherwise, a great post! Thanks Jeff!

31 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Located in Canada
  • Has collected 10+ items on Envato Market
brenelz says

There is an easier way to do it.

Remember you can do this with SQL :

DELETE FROM products WHERE product_id IN (1, 3, 4, 5)

So all you need to do with that Checkbox array is put it into a comma seperated list.

2529 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Interviewed on an Envato blog
+11 more
JeffreyWay says

Hey guys. I’m sorry – I didn’t write this one. I just forgot to assign the author. Doing it right now. :)

4 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
ripsup says

People already pointed out some major flaws with this, but also an issue is that you should be doing a Mysql error check with each query you are doing. Maybe you have issues with the first couple of queries and then none with the later ones. You would then not see any of those errors. As well it would be better to do them all in one go so you don’t have to worry about half your deletes working. This also would help reduce the queries going to the database and make things quicker overall.

Another small point, your example seems to be related to items that might be for sale. Most likely you wouldn’t want to delete them as they might be attached to something else in the database (say an invoice) that might still need that information. I run into this a lot with our student programmers who have not quite grasped the interconnection between things in the database or the desire to be able to see things in the past that are no longer needed. In cases like that it is better to have an active/disabled column that you can change instead of deleting the whole item. Deletes are something rarely used by us normally only for linking tables.

And as a double emphasis, please add sanitization even in simple example code. This is something that a lot of people still don’t quite get because they never see it in the examples they look at and is one of the worst security risks you can have.

4 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
ripsup says

Sorry to complain more about this tutorial but the live demo is pretty pointless after the first couple people delete items. :) Might want to add a repopulate button.

But on a more serious note. Why use jQuery in this case to show off that effect. You are loading there entire library to add the class even to a few rows. This could have been easily added in the loop in the original PHP . This tutorial really goes all over the place and could have been helped by maybe not trying to throw to much in especially when it’s not needed. This could have really done better in having a more solid single message instead of being all over the place.

2 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 50+ items on Envato Market
  • Located in Germany
rufnex says

You don’t need a loop for the deleting stuff. You can use the snippet that brenelz postet before or something like that:

$array = range(1,7); $and = implode(", AND id = ", $array); print "DELETE FROM table WHERE id = $and";
1 post
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
  • Located in United Kingdom
gkwelding says

easiest way to do this is have each delete checbox named like so

<input type="checkbox" name="delete[]" value={id of record} />

then in the php script do the following to escape the data:

foreach($_POST['delete'] as &$id) { $id = mysql_real_escape_string($id); } $values = array_values($_POST['delete']); $values = implode(',', $values); $sql = 'DELETE FROM {table} WHERE id IN ('.$values.')'; $query = mysql_query($sql);

this way the values are escape and you’re not creating a million database calls to do something that could be done with 1.

2 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 50+ items on Envato Market
  • Located in Germany
rufnex says

@gkwelding: that is top ;o)

960 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
jeffeatworld says
There is an easier way to do it.

Remember you can do this with SQL :

DELETE FROM products WHERE product_id IN (1, 3, 4, 5)

So all you need to do with that Checkbox array is put it into a comma seperated list.

Yep, totally agree – I think someones mentioned prepared statements, so yeah this would work really well with that.

by
by
by
by
by
by