delete multiple records in PHP

delete multiple records using checkbox php

Welcome back to shortlearner.com, in our previous post we learn How to Create pagination using php and mysql . now in this post we will learn how to delete multiple records using checkbox with the help of PHP and MySQL.

delete multiple records in PHP

before start this tutorial first of all we should understand the overall scenario of this post.
now first of all we need to create a database connection , so in this example my database name is testdb .

after creating a database connection we fetch all the records from our user table and designed it in a tabular format with checkbox field, and also create a java script function that will help us to select all check boxes at a time.
so just follow the below code.

Also Read :
Get Domain name from URL
Unable to create a directory a wordpress error
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
$conn = mysqli_connect("localhost","root","rootroot","testdb");
if(isset($_POST['save'])){
	$checkbox = $_POST['check'];
	for($i=0;$i<count($checkbox);$i++){
	$del_id = $checkbox[$i]; 
	mysqli_query($conn,"DELETE FROM user WHERE id='".$del_id."'");
	$message = "Data deleted successfully !";
}
}
$result = mysqli_query($conn,"SELECT * FROM user");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete userandroid data</title>
</head>
<body>
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<form method="post" action="">
<table class="table table-bordered">
<thead>
<tr>
    <th><input type="checkbox" id="checkAl"> Select All</th>
	<th>userandroid Id</th>
	<th>First Name</th>
	
	<th>Email id</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["id"]; ?>"></td>
	<td><?php echo $row["id"]; ?></td>
	<td><?php echo $row["user_name"]; ?></td>
	<td><?php echo $row["email"]; ?></td>
	
</tr>
<?php
$i++;
}
?>
</table>
<p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>

copy the above code and save it on htdocs directory of your xampp server.
there is a www folder if you are using WAMP or Appserv local server.

share this code with your developer buddies and if you have another way to make it simple ,please feel free to contact we will update your method also in this post.

keep coding, keep learning