Delete multiple records

Sometimes we may want to delete multiple records at once for more efficient work. We should provide checkbox for each records and users can select which records to delete.

In this example we should first create a database, a table and insert some data.
Type this sql script:
create database delsampel ;
use delsampel;
create table tabledelete (id int(5) primary key auto_increment, name varchar(50), position varchar(20), level int(2));
insert into tabledelete values('','Janet Jackson','Admin 2','8');
insert into tabledelete values('','Tommy Leo','Admin 1','5');
insert into tabledelete values('','John Connery','Admin 3','4');

then we should create 2 files
1. view.php
2. delete.php

below is the script for both files:
<?php
/*  view.php */
$host = "localhost";
$username = "root";
$password = "";
$database = "delsampel";
$connection = mysql_connect($host, $username, $password);
mysql_select_db($database, $connection) or die("MysQL Error");
$sql = mysql_query("select * from tabledelete");
?>
<html><body>
<form action="delete.php" method="POST">
<?php
echo "<table border=1>";
echo "<tr><th> </th><th>name</th><th>position</th><th>level</th></tr>";
while ($result = mysql_fetch_array($sql))
{
echo "<tr><td><input type=checkbox name=del[] id=del value=$result[id]></td><td>$result[name] </td><td> $result[position] </td><td> $result[level]</td></tr>";
}
?>
</table>
<input type="submit" name="justdel" value="Delete !!" id="justdel">
</form>
</body></html>

delete.php
<?php
/* delete.php */
$host = "localhost";
$username = "root";
$password = "";
$database = "delsampel";
$connection = mysql_connect($host, $username, $password);
mysql_select_db($database, $connection) or die("MysQL Error");
$id = $_POST[del];
$count = count($id); //counting how many rows (from checkbox) to delete
if($_POST['justdel'])
{
for ($i=0; $i<$count; $i++)
{
$sql = mysql_query("delete from tabledelete where id=$id[$i]");
}
if ($sql)
{
print "Record successfully deleted<br>";
print "<a href=view.php>Click here to go back</a>";
}
}
?>

those example files can be downloaded here!! 2kb

14 comments:

Murtaza said...

thanks for the post.

I want to ask one thing that u take table in the echo argument instead use while and then display each separately. I did this but fails wants wrong with it..

ray said...

I would like to say a huge thank you for this code it works brilliant, i had been searching for days to figure this out and what luck to land here. And I managed to get it working to also update records as well as delete, what joy. One think I would like ask is can it be that it doesn't bring in to view the delete page but rather stays on the page your deleteing from?

Anonymous said...

Thank you. After trying several (frankly crappy) scripts - here's one that works. Thanks so much.

Shahbaz Ahmed said...

Oh Thanks a Lot , i again Happy from thsi website Tutorial. Dear Sir. i have a Request i wana Show it in my wish design Table.
Like we have 2 Row and 5 Colum
in Firts Colum it will be Text in First Row Checkbox and 2nd row will have Checkbox
in @nd Colum we will write Name and 2nd Row 2nd column will show the user Name from DataBase
lease can u write in that Type. i so many try But can not do
Please it a Request
First Row shoud be Text and 2nd row for while loop fom database

Jignesh said...

I really thankful to u for this delete multiple rows tutorial I was seeking this code frm last 2 months and finally i got I'm very much happy and onceagain THANK You.

Marcelo Vieira said...

This is the only script that worked for me untill now! Thank you and congratulations!

Anonymous said...

i click delete button then it deletes all not selected areas

resty said...

i click delete button then it deletes all not selected areas
help please

resty sapio said...

an error appear



Notice: Use of undefined constant del - assumed 'del' in D:\xampp\htdocs\leandro\delete.php on line 12
Record successfully deleted

thellie said...

This works perfectly on my local server, but fails on the hostgator webserver with the if($sql) statement.

Everything, on both servers, is identical except the mySQL version (5.1.52 hostgator, 5.5.12 localserver)

Any ideas please, as it is highly useful...

Nitin....... said...

Thanks bro.....

i did this but forgot..
But i remember once again....


Wuhh..................

Nitin....... said...

Well done bro..

amara_rehman said...

its realy working . thanks a lot for this post i was searching for help n this is realy helpful.
Thanks for this post.

Pradeepanand said...

Thanks for the post.

I would like to know if there is any tutorial for inserting multiple rows using checkboxes.