Delete multiple records using checkbox in php

Create deletemultiple.php file.

<?php
$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="student";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
<table width="600" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="" onSubmit="return validate();">
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#ddd">
<tr>
<td>&nbsp;</td>
<td colspan="4"><strong>Delete Multiple Records using Checkbox in PHP</strong> </td>
</tr>
<tr><td></td></tr>
<tr>
<td></td>
<td style=" width:10%"><strong>Id</strong></td>
<td style=" width:30%"><strong>Name</strong></td>
<td style=" width:20%"><strong>Class</strong></td>
<td style=" width:40%"><strong>Email</strong></td>

</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" 
value="<?php echo $rows['id']; ?>"></td>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['class']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php

// Check if delete button active, start this
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletemultiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Vineet Kumar Saini

 

 

 

 

 

 

 

When you click on delete button without select any checkbox then can’t delete i.e.

Vineet Kumar Saini

 

 

 

 

 

 

 

 

Now you can select any check box then click on delete button then

Vineet Kumar Saini

 

 

 

 

 

 
Delete multiple records using checkbox in php

Advertisements
Categories: Joomla, PHP & Joomla | Tags: , , , , , , | 19 Comments

Post navigation

19 thoughts on “Delete multiple records using checkbox in php

  1. keerthana

    this article is really amazing and its working also thanks for your sharing

  2. keerthana

    i have a doubt on this line.what is the use of form name.how the delete button knows the form.pl help

    thanks in advance

  3. Hi sir,

    This code is really amazing and its working.
    thank you for sharing…………

    Sanajy Shelar

  4. thank you so much sir,

  5. Jatin nagendra

    thanks

  6. shingala sohil

    thanks

  7. gaurav

    bro i want insert update delete sorting paggination on one page with single n multyple

  8. thanks dude.. i’ve been looking for simple and working codes. and u help me with thid.not just it understandable.. it also WORKING!!

  9. Rohit Gour

    thanks for this code

  10. samuel

    worked perfectly

  11. Monika

    thanks a ton…

  12. deepmala

    Thanks really nice artical

  13. noel

    Excellent code….neat and good

  14. rajesh patidar maheshwar

    thanks

  15. THANKS U THANKS U ,U HELP ME A LOT

  16. Thanks a ton. It’s really helpful. Good work there.!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: