Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Delete Multiple Records From MySQL Using PHP

Tuesday, Mar 7, 2017

Tags:- PHP MySQL


In this tutorial we will show you how to delete multiple records from MySQL using PHP by simply checking the check box of rows you want to delete and submit the form using this method you can delete single and multiple records at a time.You may also like add, edit and delete records from MySQL using PHP and ajax.


Delete Multiple Records From MySQL Using PHP


To Delete Multiple Records It Takes Only Two Step:-

  1. Make a PHP file to display records for delete
  2. Make a PHP file to delete selected records


Step 1.Make a PHP file to display records for delete

We make a PHP file and save it with a name display_records.php

// Database Structure 
CREATE TABLE `user_detail` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` text NOT NULL,
 `age` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<html>
<body>
<div id="wrapper">

<?php
$host="localhost";
$username="root";
$password="";
$databasename="sample";
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

$select =mysql_query("SELECT * FROM user_detail");
?>

<form method="post" action="delete_records.php">
 <table align="center" cellpadding="10" border="1">
 <tr>
  <th></th>
  <th>NAME</th>
  <th>AGE</th>
 </tr>
 <?php
 while ($row=mysql_fetch_array($select)) 
 {
  echo "<tr>";
   echo "<td><input type='checkbox' name='no[]' value='".$row['id']."'></td><td>".$row['name']."</td><td>".$row['age']."</td>";
  echo "</tr>";
 }
 ?>
 </table>
 <input type="submit" name="delete_records" value="Delete Records">
</form>

</div>
</body>
</html>

In this step we create a database table called 'user_detail' to display and delete records.Then we display all the records user can check the single or multiple checkbox to delete.You may also like prevent multiple and duplicate form submission using PHP.



Step 2.Make a PHP file to delete selected records

We make a PHP file and save it with a name delete_records.php

<?php
if(isset($_POST['delete_records']) && count($_POST['no'])>0)
{
$host="localhost";
$username="root";
$password="";
$databasename="sample";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

for($i=0;i<count($_POST['no']);$i++)
{
 $row_no=$_POST['no'][$i];
 mysql_query("delete from user_detail where id='$row_no'");
}
}
?>

In this step we get all the values from 'no' array which contains the records id and then delete all the records having these values using forloop().You may also like sort MySQL table using PHP.



Thats all, this is how to delete multiple records from mysql using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH