Delete multiple records with PHP and MySql

Delete multiple records with PHP and MySql

In my last tutorial I had posted how to Check/Uncheck all checkbox with Jquery, resuming from where I left, in this tutorial I am going to show how to implement that check/uncheck feature to delete multiple records with PHP and MySql. User can select as many checkbox and corresponding to that records will be deleted from the database.

View Demo

Scope of the script?

Before proceeding further let me just tell you that in this article my main focus will be on PHP and MySQL part. If you want to learn how to check/uncheck all checkboxes have a look at this post. This scripts deletes records from the database based on the user’s checkbox selection.

Requirements before running this script?

In order to run this script make sure that you have properly setup your database configuration value in constants.php. Also make sure that your table has sufficient records. If you don’t have a table, try to execute the sql query that I have provided in db.sql in your download file. Executing that file will create a table and add some demo records in your database.

Let’s create and deleting records now

1. First things first, create a table and insert some demo values. Use db.sql file to do that.

  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)

INSERT INTO `tbl_demo3` (`id`, `name`) VALUES
(9, 'demo9'),
(8, 'demo8'),
(7, 'demo7'),
(6, 'demo6'),
(5, 'demo5'),
(4, 'demo4'),
(3, 'demo3'),
(2, 'demo2'),
(1, 'demo1'),
(100, 'demo100');

2. Make sure that your checkbox name attribute is an array. You can give whatever name you want but just end it with a [] sign.
<form name="f" method="post" action="index.php">
<input type="submit" name="sub" value="Delete" onClick="javscript:return confirm('Are you sure you want to delete?');" >
$query = mysql_query($sql);
 <?php while($rs = mysql_fetch_object ($query)){  ?>
            <td style="text-align:center;"><input type="checkbox" name="ids[]" class="checkboxes" value="<?php echo stripslashes($rs->id); ?>" ></td>
            <td style="text-align:center;"><?php echo stripslashes($rs->name); ?> </td>
<?php } ?>

3. When you submit the form, collect all the checkbox values and implode their ids in a string with a comma(,) seperator.
if (count($_POST["ids"]) > 0 ) {
  $all = implode(",", $_POST["ids"]);

4. Now just fire a delete query with an IN clause and you are done.
$sql = "DELETE FROM ".TABLE_DEMO." WHERE 1 AND id IN($all)";
if ( @mysql_query($sql)) {
  $errmsg = ("Rows has been deleted successfully");
} else {
  $errmsg = ("Error while deleting.". mysql_error());			}

Hope this post gives a clear view on how to delete multiple records with php and mysql. Share your feedback/ comments below.
View Demo
Share to download the Source Codes for FREE!
We're glad to give free downloads, but we need your love to carry on making that.
Please support us by sharing the page.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.


Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

shivasurya on

cool tutorial!

karthikeyan on

Awesome tutorial. My requirement differs far away. Let say, i have text boxes on each row and want to send text box values of which check boxes are checked. Ids[] stores checked check box value but tval[] will store all text boxes values. Any solution please? i tried with two dimensional array tval[$id][] and need to extract only idsl[] contents (id’s) from tval[][] but can’t accomplish it. Any help?

Shahrukh Khan on

Does the value of text boxes comes from databases like
id1 textboxval1
id2 textboxval2
id3 textboxval3

karthikeyan on

no, i need to enter text box values dynamically but id will be from database.

Shahrukh Khan on

OK, Send me an email with this topic as a subject. I will send you the script.

amey on

Great work bro !!!

Shahrukh Khan on

Thank You

Mantu Kumar on

When id Click on Demo Link

Fatal error: Call to a member function prepare() on null in /home3/thesoehc/public_html/demos/delete-multiple-records-php/index.php on line 71

Shahrukh Khan on

Thanks I have fixed that issue.

Ren on

Not working for me. doesn’t done anything


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.