How to delete multiple records in PHP
MySQL Table Structure for this tutorial
CREATE TABLEFetch the data from the database and create checkbox . Checkbox name attribute is an array.tblusers(idint(11) NOT NULL,FullNamevarchar(140) DEFAULT NULL,Educationvarchar(120) DEFAULT NULL,postingDateint(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLEtblusersADD PRIMARY KEY (id); ALTER TABLEtblusersMODIFYidint(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
<table class="table table-striped custab">
<!-- Deletion Button -->
<tr>
<td colspan="4"> <input type="submit" name="submit" value="Delete" class="btn btn-primary btn-md pull-left" onClick="return confirm('Are you sure you want to delete?');" ></td>
</tr>
<tr>
<th>
<!-- For Selecting All -->
<li><input type="checkbox" id="select_all" /> Select all</li></th>
<th>Name</th>
<th>Education </th>
<th>Date</th>
</tr>
<?php
$query=mysqli_query($con,"select * from tblusers");
$totalcnt = mysqli_num_rows($query);
if ($totalcnt > 0) {
while ($row=mysqli_fetch_array($query)) {
?>
<tr>
<td><input type="checkbox" class="checkbox" name="ids[]" value="<?php echo htmlentities($row['id']);?>"/></td>
<td><?php echo htmlentities($row['FullName']);?></td>
<td><?php echo htmlentities($row['Education']);?></td>
<td><?php echo htmlentities($row['postingDate']);?></td>
</tr>
<?php } } else { ?>
<tr>
<td colspan="4"> No Record Found</td>
</tr>
<?php } ?>
</table>
On Submitting the from collect all checkbox ids and implode their ids
if (isset($_POST["submit"])) {
if (count($_POST["ids"]) > 0 ) {
// Imploding checkbox ids
$all = implode(",", $_POST["ids"]);
$sql =mysqli_query($con,"DELETE FROM tblusers WHERE id in ($all)");
if ($sql) {
$errmsg ="Data has been deleted successfully";
} else {
$errmsg ="Error while deleting. Please Try again.";
}
} else {
$errmsg = "You need to select atleast one checkbox to delete!";
}
}
HERE IS THE FULL CODE THAT WE HAVE WRITTEN DURING THIS TUTORIAL :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
<?php include('dbconfig.php'); error_reporting(0); if (isset($_POST["submit"])) { if (count($_POST["ids"]) > 0 ) { // Imploding checkbox ids $all = implode(",", $_POST["ids"]); $sql =mysqli_query($con,"DELETE FROM tblusers WHERE id in ($all)"); if ($sql) { $errmsg ="Data has been deleted successfully"; } else { $errmsg ="Error while deleting. Please Try again."; } } else { $errmsg = "You need to select atleast one checkbox to delete!"; } } ?> <!DOCTYPE html> <html> <head> <meta name="description" content="How to delete multiple record in PHP"> <meta name="keywords" content="How to delete multiple record in PHP"> <meta name="author" content="Anuj Kumar"> <title>How to delete Multiple Data in PHP</title> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <style type="text/css"> .custab{ border: 1px solid #ccc; padding: 5px; margin: 5% 0; box-shadow: 3px 3px 2px #ccc; transition: 0.5s; } .custab:hover{ box-shadow: 3px 3px 0px transparent; transition: 0.5s; } li { list-style-type: none; } </style> </head> <body> <form name="multipledeletion" method="post"> <div class="container"> <div class="row col-md-6 col-md-offset-2 custyle"> <h2>How to delete Multiple Record in PHP</h2> <!-- Message --> <p style="color:red; font-size:16px;"> <?php if($errmsg){ echo $errmsg; } ?> </p> <table class="table table-striped custab"> <!-- Deletion Button --> <tr> <td colspan="4"> <input type="submit" name="submit" value="Delete" class="btn btn-primary btn-md pull-left" onClick="return confirm('Are you sure you want to delete?');" ></td> </tr> <tr> <th> <!-- For Selecting All --> <li><input type="checkbox" id="select_all" /> Select all</li></th> <th>Name</th> <th>Education </th> <th>Date</th> </tr> <?php $query=mysqli_query($con,"select * from tblusers"); $totalcnt = mysqli_num_rows($query); if ($totalcnt > 0) { while ($row=mysqli_fetch_array($query)) { ?> <tr> <td><input type="checkbox" class="checkbox" name="ids[]" value="<?php echo htmlentities($row['id']);?>"/></td> <td><?php echo htmlentities($row['FullName']);?></td> <td><?php echo htmlentities($row['Education']);?></td> <td><?php echo htmlentities($row['postingDate']);?></td> </tr> <?php } } else { ?> <tr> <td colspan="4"><a href="rollback.php"> Roll back all data</a></td> </tr> <tr> <td colspan="4"> No Record Found</td> </tr> <?php } ?> </table> </div> </div> </form> <script type="text/javascript"> $(document).ready(function(){ $('#select_all').on('click',function(){ if(this.checked){ $('.checkbox').each(function(){ this.checked = true; }); }else{ $('.checkbox').each(function(){ this.checked = false; }); } }); $('.checkbox').on('click',function(){ if($('.checkbox:checked').length == $('.checkbox').length){ $('#select_all').prop('checked',true); }else{ $('#select_all').prop('checked',false); } }); }); </script> </body> </html> |
View Demo——————-
