How to Delete Multiple data from database using checkbox in PHP MySQL
By Super Admin |
Jun 10, 2021 |
PHP
How to delete multiple data in php using Checkbox
In this post, you will learn how to delete multiple data from database using checkbox in PHP MySQL. So guys, give form tag and inside that we will design a HTML table and then fetch the students data in table row (<tr>) with checkbox input, to delete multiple data by checking the multiple checkboxes.
We will be using Bootstrap 5 version to design the user interface.
So, Lets get started:
Step 1: Create a table named student as follows: (Insert few data to fetch record in html table so that you can delete multiple data in php mysqli):
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stud_name` varchar(191) NOT NULL,
`stud_class` varchar(100) NOT NULL,
`stud_phone` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
);
Step 2: Create a file named index.php and paste the below code as follows:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Funda of Web IT</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card mt-5">
<div class="card-header">
<h4>How to Delete Multiple Data or record using Checkbox in PHP MySQL</h4>
</div>
</div>
</div>
<div class="col-md-12">
<?php
if(isset($_SESSION['status']))
{
?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
unset($_SESSION['status']);
}
?>
<div class="card mt-4">
<div class="card-body">
<form action="code.php" method="POST">
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th>
<button type="submit" name="stud_delete_multiple_btn" class="btn btn-danger">Delete</button>
</th>
<th>ID</th>
<th>Name</th>
<th>Class</th>
<th>Phone No</th>
</tr>
</tbody>
<tbody>
<?php
$con = mysqli_connect("localhost","root","","phptutorials");
$query = "SELECT * FROM student";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0)
{
foreach($query_run as $row)
{
?>
<tr>
<td style="width:10px; text-align: center;">
<input type="checkbox" name="stud_delete_id[]" value="<?= $row['id']; ?>">
</td>
<td><?= $row['id']; ?></td>
<td><?= $row['stud_name']; ?></td>
<td><?= $row['stud_class']; ?></td>
<td><?= $row['stud_phone']; ?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="5">No Record Found</td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 3: Create a file named code.php and paste the below code as follows:
<?php
session_start();
$con = mysqli_connect("localhost","root","","phptutorials");
if(isset($_POST['stud_delete_multiple_btn']))
{
$all_id = $_POST['stud_delete_id'];
$extract_id = implode(',' , $all_id);
// echo $extract_id;
$query = "DELETE FROM student WHERE id IN($extract_id) ";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$_SESSION['status'] = "Multiple Data Deleted Successfully";
header("Location: index.php");
}
else
{
$_SESSION['status'] = "Multiple Data Not Deleted";
header("Location: index.php");
}
}
?>
Thanks for reading...