This tutorial shows how to delete multiple MySQL records at once using checkboxes, a jQuery "check all" helper, and PHP on the backend.
jQuery — Check All & Delete Confirmation
Include jQuery and attach two click handlers: one that toggles all checkboxes, and one that counts selected rows and shows a confirmation before the form submits.
<script src="jquery.js"></script>
<script>
jQuery(function($) {
// Check / uncheck all checkboxes when "Check All" is clicked
$("form input[id='check_all']").click(function() {
var inputs = $("form input[type='checkbox']");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].getAttribute("type") == "checkbox") {
inputs[i].checked = this.checked;
}
}
});
// Confirm before deleting; block if nothing selected
$("form input[id='delete']").click(function() {
var count_checked = $("[name='checkbox[]']:checked").length;
if (count_checked == 0) {
alert("Please select a product(s) to delete.");
return false;
}
if (count_checked == 1) {
return confirm("Are you sure you want to delete this product?");
} else {
return confirm("Are you sure you want to delete these products?");
}
});
}); // jQuery end
</script>
PHP — Display Rows & Process Deletion
Connect to the database, fetch all rows with checkboxes, and delete the selected IDs when the form is submitted.
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "practice";
$tbl_name = "test";
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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5"><strong>Delete multiple rows in MySQL</strong></td>
</tr>
<tr>
<td>Check All <input type="checkbox" id="check_all" value=""></td>
<td><strong>Id</strong></td>
<td><strong>Name</strong></td>
<td><strong>Lastname</strong></td>
<td><strong>Email</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>"></td>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php } ?>
<tr>
<td><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</table>
</form>
<?php
// Process deletion when Delete button is clicked
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);
}
// Refresh page on success
if ($result) {
echo '<meta http-equiv="refresh" content="0;URL=delete_multiple.php">';
}
}
mysql_close();
?>
</td></tr>
</table>
How It Works
- The "Check All" checkbox triggers a jQuery loop that sets every row checkbox to match its state.
- The Delete button counts selected checkboxes; shows an alert if zero, or a confirm dialog otherwise.
- PHP loops through
$_POST['checkbox']and runs aDELETE FROMquery for each ID. - A meta-refresh redirects back to the list after a successful delete.
Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.