In this post, we have shared How To Delete Multiple Selected Rows Using checkbox in PHP without Ajax. normally we have added the button or anchor tag for delete single data. but when we have large data and we will delete the single data, at that time it will take more time. Then at that time, this functionality will helpful.

For discuss, How To Delete Multiple Selected Rows Using checkbox in PHP without Ajax. after then configuration database and fetch the data and also we will have set bootstrap and take the form and table. here pass data into TR and TD tag through 'WHILE' loop and also take checkbox with multiple names in every single row.

When we will have checked the checkbox and then after click on the delete button then the form will be submitted. it will count the number of checkboxes which are checked. it will get the checkbox value using the for loop after then delete data one by one.

See below example of How To Delete Multiple Selected Rows Using checkbox in PHP without Ajax.

<?php
 $hostname="localhost";
 $username="root";
 $password="";
 $database="php";
 $conn = mysqli_connect($hostname,$username,$password,$database);
 
if(isset($_POST['btnDelete']))
{
	$checkbox = $_POST['check'];
	for($i=0;$i<count($checkbox);$i++)
	{
		$del_id = $checkbox[$i]; 
		mysqli_query($conn,"delete from users where id='".$del_id."'");
		$message = "Data Successfully Deleted";
	}
}
$result = mysqli_query($conn,"select * from users");
?>
<!DOCTYPE html>
<html>
<head>
	<title>How To Delete Multiple Selected Rows Using checkbox</title>
    <meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
	<div class="col-lg-12">
		<?php if(isset($message)) { ?>
		<div class="alert alert-success alert-dismissible">
		  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>	
		  <strong>Success!</strong> <?php echo $message; ?> </strong>
		</div>
		<?php } ?>
	</div>
</div>

<form method="post" action="">
	<div class="row">
		<div class="col-lg-12">
			<button type="submit" class="btn btn-danger btn-sm" name="btnDelete">DELETE</button>
		</div>
	</div>
	<br/>
	<table class="table table-bordered">
		<thead>
		<tr>
			<th><input type="checkbox" id="checkAll"> <label for="checkAll">Select All</label></th>
			<th>Id</th>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Email</th>
			<th>Created Date</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["first_name"]; ?></td>
			<td><?php echo $row["last_name"]; ?></td>
			<td><?php echo $row["email"]; ?></td>
			<td><?php echo $row["created"]; ?></td>
		</tr>
		<?php
		$i++;
		}
		?>
	</table>
</form>
</div>
<script>
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>

I have shared demo Button or URL. So you can click on Button or URL and show the demo.

Show Demo

Connect With Facebook