PHP Mysql-Deleting multiple records using checkbox

In: PHP

9 Aug 2009

Sometimes we have to delete multiple records at once. There is various method to delete multiple records at once. Here I am going to show a very simple method using checkbox to delete multiple records. I hope it will help you.

First create a database, here I am goint to create a database called test.

CREATE DATABASE `test` ;

Now create a table to insert data.

CREATE TABLE `test`.`tbluser` (

  `userid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,

  `name` VARCHAR( 100 ) NOT NULL ,

  `address` VARCHAR( 100 ) NOT NULL ,

  `contactno` INT( 7 ) NOT NULL ,

  `email` VARCHAR( 100 ) NOT NULL ,

  `remarks` TEXT NOT NULL 

) ENGINE = MYISAM

OK Now write your connection string to connect with mysql database:

<?php

$dbhost="localhost";

$dbuser="yourdbusername";

$dbpass="yourdbpassword";

$dbname="test";

mysql_connect("$dbhost","$dbuser","$dbpass") or die('Could not connect');

mysql_select_db($dbname);

?>

Now create a form and display all data and use a checkbox having value of userid.

Here I will pass value in array using checkbox

<input name=”checkbox[]” type=”checkbox” id=”checkbox[]” value=”<? echo $row['userid']; ?>”>

Now insert submit button for delete records

<input name=”delete” type=”submit” id=”delete” value=”Delete Records”>

Using For loop find the selected checkbox and delete records

<?

Check if Delete button was posted

if($_POST['delete']){

Check for posted checkboxes

$checkbox=$_POST['checkbox'];

Using the for loop find the checkboxes which are selected

for($i=0;$i<count($checkbox);$i++){

Pass the Value in $del_id variable

$del_id = $checkbox[$i];

Now Delete records from table

$sql = “DELETE FROM tbluser WHERE userid=’$del_id’”;

$result = mysql_query($sql);

}

And now refres the page

if($result){

echo “<meta http-equiv=\”refresh\” content=\”0;URL=deletemultiplerecords.php\”>”;

}

}

?>

Complete Code:

<?php
 $dbhost="localhost";
   $dbuser="yourdbuserid";
   $dbpass="yourdbpassword";
   $dbname="test";
mysql_connect("$dbhost","$dbuser","$dbpass") or die('Could not connect');
mysql_select_db($dbname);
?>
<form name="form1" method="post" action="">
<table width="918" border="0">
<tr>
<td>SNO</td>
<td>Name</td>
<td>Address</td>
<td>Contact No</td>
<td>Email</td>
<td>Remarks</td>
<td>Select</td>
</tr>
<?php

  $query="SELECT * FROM tbluser";

  $result=mysql_query($query);

  $sno=1;

  while($row=mysql_fetch_array($result, MYSQL_ASSOC)){

  ?>
<tr>
<td><? echo $sno;?></td>
<td><? echo $row['name'];?></td>
<td><? echo $row['address'];?></td>
<td><? echo $row['contactno'];?></td>
<td><? echo $row['email'];?></td>
<td><? echo $row['remarks'];?></td>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
</tr>
<?

  $sno=$sno+1;

  }

  ?>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2"><div align="center">
<input name="delete" type="submit" id="delete" value="Delete Records">
</div></td>
</tr>
</table>
<?

  if($_POST['delete']){

  //print_r($_POST);

  $checkbox=$_POST['checkbox'];

  //exit;

  for($i=0;$i<count($checkbox);$i++){

  $del_id = $checkbox[$i];

  $sql = "DELETE FROM tbluser WHERE userid='$del_id'";

  $result = mysql_query($sql);

  }

  // if successful redirect to delete_multiple.php

  if($result){

  echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletemultiplerecords.php\">";

  }

  }

  ?>
</form>

Download Zip Here

Related Articles

30 Responses to PHP Mysql-Deleting multiple records using checkbox

Avatar

Delete Multiple Records Using Checkbox With Select All Buttons | Theme Heven

August 9th, 2009 at 9:17 am

[...] All and Unselect Features. I have simply added a javascript code in my previous post which you can see here. All of the Features are same as my previous code and I am using the new codes which I added on my [...]

Avatar

Srinivasu Neti

August 17th, 2009 at 7:53 am

Hi, your article is useful.
I have one question to you. I could able to populate the html table with db rows with an additional checkbox for each row to identify which row the user has selected. Issue is I want to know the value of particular column in selected row. for eg., I populate a textbox with db row’s column say “Amount” . if I loop thru the post["
checkboxArray"], i cannot be able to retrieve the corresponding textbox value where the amount is modified. I’m using while() with current() function after posting the page. Please help me out in this regard.

Thanks in advance,
Srinivas N.

Avatar

admin

August 18th, 2009 at 1:46 am

Thank you Neti, Sure I will post one article on your requirement. Thank you.

Avatar

hawraz

August 26th, 2009 at 12:13 pm

hi. i want to thank you in advance it is very very useful thanks for your job

hawraz from kurdistan region of iraq

best wishes hope all the best to you.

Avatar

hawraz

August 26th, 2009 at 1:45 pm

hi

but can you also add paging to the above code if it is not mind because sometimes there will be alot of data so it is not possible to see them in one page there should be other pages for simplicity. i want that because i spent alot of time in this problem.
it will be appreciated if you do that
thanks again
wish you success.

Avatar

admin

August 26th, 2009 at 10:36 pm

Ok Thank you I will replay it in the same project. Thanks for your wish.

Avatar

hawraz

August 27th, 2009 at 1:55 am

thanks again.

Avatar

hawraz

August 27th, 2009 at 2:02 am

thanks again

Avatar

hawraz

August 27th, 2009 at 2:09 am

waiting for that, sorry for the disturbtion

Avatar

hawraz

August 27th, 2009 at 3:32 am

hi
i did the paging so dont do it. but in your updated version of deletemultiplerecords.php it give the following error or notice could u tell me what is it or how to solve it i tried alot but no result.

Notice: Undefined index: delete in c:\program files\easyphp1-8\www\mrov_company\deletemultiplerecords.php on line 70

Avatar

seven5seven

September 18th, 2009 at 2:35 am

Brilliant tutorial, worked a treat! Thanks so much :)

Avatar

xeraNin3

September 28th, 2009 at 3:04 pm

Thank you!!! This tutorial really helped me in making an image gallery that uses checkbox to delete multiple images… tnx a lot!!! (^_^)

Avatar

Colin

October 22nd, 2009 at 2:15 pm

Hello,
For some reason I can’t get the check boxes to contain any value, therefore when I select delete no rows are deleted. Could you please help me with this?
Thanks,
-Colin

Avatar

admin

October 24th, 2009 at 1:27 pm

@Colin: Thank you Colin. Can you post your code please.

Avatar

Arram

November 27th, 2009 at 12:14 pm

Thanks a lot for this tutorial, it worked like a charm.

Avatar

rafal

February 24th, 2010 at 10:10 am

dzieki:*

Avatar

junior

February 24th, 2010 at 10:28 pm

realmente muy util ….thanks xD

Avatar

Lei

March 27th, 2010 at 7:39 pm

In my localhost not work, any something to edit my local server?

Avatar

Lei

March 27th, 2010 at 8:38 pm

sorry it’s work full… thanks.

regards,
Lei

Avatar

engin

May 12th, 2010 at 3:08 pm

Hi I have a request. you create for deleting but I want to insert to database which checkbox is checked

Avatar

admin

May 17th, 2010 at 2:35 am

Thanks But I don’t understand what you say you want to insert value of check box or ???

Avatar

Checkbox inset problem

July 20th, 2010 at 4:21 am

I Want to know how to insert checked rows into MySql database.

Avatar

Jay

July 25th, 2010 at 9:16 am

Hi Everybody,

Here in the first program as it is i have copied and run it. It is displaying the details but unable to delete the selected details.

What could be the reason. Major problem is at for loop only. Let me know the reason any

Avatar

admin

July 25th, 2010 at 8:37 pm

Same as delete run your insert query to solve your problem. If you still feel problem do not hesitate to ask suggestions.

Avatar

admin

July 25th, 2010 at 8:38 pm

@jay: Can you post your code?

Avatar

Alonso

August 9th, 2010 at 10:59 am

I wasn’t able to get the delete part working either, I am not sure why because it seems fine.

Below is my code, if anybody is able to point me in the right direction I would appreciate it. Thanks!

SNO
Name
Address
Contact No
Email
Remarks
Select

<input name=”checkbox[]” type=”checkbox” id=”checkbox[]” value=”">

 
 
 
 
 

<?php
if($_POST['delete']){
//print_r($_POST);
$checkbox=$_POST['checkbox'];
//exit;
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = “DELETE FROM test WHERE userid=’$del_id’”;
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo “”;
}
}
?>

 

Avatar

admin

August 12th, 2010 at 2:26 am

your error is here
<input name=”checkbox[]” type=”checkbox” id=”checkbox[]” value=””
>
Post value to delete.

Avatar

Alonso

August 24th, 2010 at 9:34 am

It seems that I didn’t post my code properly sorry about that, that was not the problem however I was able to solve it.

haha it was a silly mistake I forgot to switch
<? echo $row['userid']; ?
to
<?php echo $row['userid']; ?

lol anyways thanks for your help, I really like the article :D

Avatar

hadi

August 28th, 2010 at 11:27 pm

Hi,

I get this error
Parse error: syntax error, unexpected $end in C:\Users\acer\Desktop\xampp\htdocs\miniprojek\deletemultiplerecords.php on line 81

I just download your zip file and put it in my htdocs. No change at all. Any help?

Thank you.

Avatar

admin

September 2nd, 2010 at 9:17 pm

@hadi: please check your code you may have deleted one “}” bracket.

Comment Form

  • admin: @hadi: please check your code you may have deleted one "}" bracket. [...]
  • hadi: Hi, I get this error Parse error: syntax error, unexpected $end in C:\Users\acer\Desktop\xampp\h [...]
  • SF: High resolution zoom with image tiles: http://www.ajax-zoom.com [...]
  • Alonso: It seems that I didn't post my code properly sorry about that, that was not the problem however I wa [...]
  • admin: your error is here <input name=”checkbox[]” type=”checkbox” id=”checkbox[]” [...]
Buy Stock Flash | Buy and Sell Royalty-Free Flash effects music and video files

Free web hostingWeb hosting
Display Pagerank