how to delete multiple records in sql with demo

how to delete multiple records in sql using with demo

by Code4berry. Last modified on April 1st, 2022.

We are well mastery with PHP CRUD activities by getting to MySQL by means of PHP rationale. However, we have seen about how to refresh and erase table columns each in turn. This article manages choosing numerous columns for applying delete activities.

For choosing numerous columns, we will utilize checkbox input for submitting chosen lines reference. We can get chosen checkbox values through jQuery AJAX post or by getting to the structure from PHP after page revive.

Steps on how to delete multiple records in sql

While implementing multiple rows delete, we need to follow some steps. All the steps listed below are suitable for deleting multiple rows.

  1. Selecting rows using checkbox input.
  2. You can also click in select all to select all rows.
  3. Submit array of row details to PHP.
  4. Repeat through line subtleties exhibit to apply erase inquiry for each.
how to delete multiple records in php

First, let us start coding for deleting multiple rows.

Multiple Rows delete using Checkbox

By checking select all check box, you will be selecting all rows and they can deleted once.

Listing Database Result with Checkboxes to Allow Bulk Select

For every clients column, we really want to add checkbox input while emphasizing over a loop with the query result. So, we should create another column with user’s list view for checkbox input.

So, while printing client table passages powerfully to the rundown view, the circle ought to contain the accompanying things.

Source code

dbconfig.php

								
									<?php
									define('DB_SERVER','localhost');
									define('DB_USER','root');
									define('DB_PASS' ,'');
									define('DB_NAME', 'mdel');
									$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
									// Check connection
									if (mysqli_connect_errno())
									{
										echo "Failed to connect to MySQL: " . mysqli_connect_error();
									}
									?>
								
							

index.php

Deleting Multiple Rows using PHP

Erase is extremely straightforward contrasted with update. Since we really want not show any UI and it requires just the variety of chosen column ids.

For multi-line erase additionally we are submitting chosen lines by utilizing Javascript. As displayed in the accompanying content, we can utilize Javascript affirm() work, to get affirmation before erase.

								
									<?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!";
					}  
				}
				if(isset($_POST['save']))
				{
					$name=$_POST['name'];
					$education=$_POST['education'];
					$year=$_POST['year'];
					$query=mysqli_query($con,"insert into tblusers(FullName,Education,PostingDate) values('$name','$education','$year')");
					if($query)
					{
						echo "<script>alert('Data inserted successfully');</script>";
					}
					else
					{
						echo "<script>alert('Data not inserted');</script>";
					}
				}
				?>
				<!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="code4berry">
				<title>How to delete Multiple Data in PHP</title>
				<!--Bootstrap -->
				<link rel="stylesheet" href="css/bootstrap.css" type="text/css">

				</head>
				<body>
				<div class="container">
				<h3 class="text-center mt20 mb5">How to delete Multiple Record in PHP</h3>
				<div class="card col-md-6 my-6 ">
				<form  method="post" enctype="multipart/form-data">
				<div class="row ">
				<div class="form-group col-md-12">
				<label >Names</label>
				<input type="text" type="text" id="name" name="name"  class="form-control" required />
				</div>
				</div>
				<div class="row ">
				<div class="form-group col-md-12">
				<label>Course</label>
				<input type="text" id="education" name="education"  class="form-control" required />
				</div>
				</div>
				<div class="row ">
				<div class="form-group col-md-12">
				<label >Year</label>
				<input type="text" id="year" name="year"  class="form-control" required />
				</div>
				</div>
				<button type="submit" name="save"  class="btn btn-sm btn-primary  mr-2 mb-4">Save</button>
				</form>
				</div>
				<div> </div>
				<form name="multipledeletion" method="post">
				<div class="row col-md-12">
				<!-- Message -->
				<p style="color:red; font-size:16px;">
				<?php if($errmsg){ echo $errmsg; } ?>  </p>
				<table class="table table-bordered">
				<!-- 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>
				<!-- For Selecting All -->
				<th> <input type="checkbox" id="select_all" /> Select all</th>
				<th>Names</th>
				<th>Course</th>
				<th>Year</th>
				</tr>
				<?php
				$query=mysqli_query($con,"select * from tblusers order by id desc");
				$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>
	</div>
	</div>
	</form>
	</div>
	<!-- Loading Scripts -->
	<script src="js/jquery.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<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>

Download

Comments

Drone Maps

Beautiful code is working properly

John Simith

Great job thank you

Van Fred

This is only learning platform with latest codes thank you

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More