Like Us on Facebook

Delete Multiple Rows from MySQL using PHP with Checkbox

This post is about Delete Multiple Rows from MySQL using PHP with Checkbox. Deleting row one by one from MySQL table is very time consuming. It will be easy, when user delete multiple rows on a single click. Actually you can select multiple rows with checkbox and using PHP you can delete all rows from MySQL table.

So I am going write this tutorial. And this tutorial is very easy. If you understand once you can easily implement into your web page. So lets start..

Delete multiple rows from MySQL using PHP with Checkbox UandBlog

index.php

This is your home page. Here you can see all user details. You can delete these rows one by one. And also you can select multiple rows with checkbox. Actually this cehckbox shows each row of MySQL table. You can also select all rows on a single click which is check all at the bottom.

After selection of your rows, its time to delete your data. So after click delete button, show_confirm22() function will call and a confirmation popup will show and ask, delete your data or not. If you click yes then that row or rows will be deleted parmanently.
 

<?php
    include_once('config.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>
Delete multiple rows from MySQL using PHP with Checkbox - UandBlog</title>


<script>
function show_confirm22()
    {
    var r=confirm("The entry will be parmanently deleted!");
    if (r==true)
    {return true;
    }
    else
    {
    return false;
    }

    }
</script>
    
</head>

<body>

<div style="width:500px ; margin:0 auto; margin-top:150px;">

 <form method="post" action="deleteMultipleData.php">

 <table width="100%" border="0" cellspacing="0" cellpadding="5" style="text-align:center;">
  <tr bgcolor="#df4662" style="color:#FFFFFF;">
    <td>
#</td>
    <td>
Id</td>
    <td>
Name</td>
    <td>
Age</td>
    <td>
Email</td>
  </tr>

  <?php
  $data = mysql_query("select * from data");
  while($fetdata = mysql_fetch_assoc($data))
  {
  ?>
  <tr>
     <td><input type="checkbox" name="delid[]" class="case" value="
<?php echo $fetdata['id']; ?>" /></td>
   
<td><?php echo $fetdata['id']; ?></td>
    <td><?php echo $fetdata['name']; ?></td>
    <td><?php echo $fetdata['age']; ?></td>
    <td><?php echo $fetdata['email']; ?></td>
  </tr>
  <?php
  }
  ?>
</table>

    <div style="margin-top:20px;">
         <div style=" float:left;">
          
<input name="delete" type="submit" id="delete" value="Delete" onClick="return show_confirm22();" style="background:#F52126; color:#fff; border:none; font-size:12px; padding:4px 8px;">
          
&nbsp;<input type="checkbox" id="selectall"/> Check All
         </div>
         <div style="clear:both;"></div>
    </div>

    
 </form>            
</div>

</body>
</html>
 

Checkbox function

This is the main function for all row selection using checkbox. Using $("#selectall").click(function () function on a single click, all rows will be checked. For this function you need a jQuery library, which is jQuery min library. You have to add this at first then you have to write check all function.


<!--check box selection-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script>
<script language="javascript">
    $(function(){
    // add multiple select / deselect functionality
    $(
"#selectall").click(function () {
          $('.case').attr('checked', this.checked);
    });
    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".case").click(function(){

        if($(".case").length == $(".case:checked").length) {
            $(
"#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }

    });
});
</script>

<!--check box selection-->


config.php

This file will connect to the MySQL database.
 
<?php
error_reporting(E_ALL && ~E_NOTICE);
mysql_connect("localhost","root","");
mysql_select_db("your_db_name");
?>
 

deleteMultipleData.php

When the delete button is clicked. Then form action redirect to this page with ID which will be deleted and this ID or multiple IDs store in an array variable. Using mysql_query, delete all rows from MySQL table using PHP. 
 
<?php
include("config.php");
$delid = $_POST['delid'];
$nc = count($delid);
for($i=0;$i<$nc;$i++)
{
    $did = $delid[$i];
    mysql_query("DELETE FROM data WHERE id='$did'");

}
?>
<script>window.location = 'index.php';</script>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.