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.
To Delete Multiple Records It Takes Only Two Step:-
- Make a PHP file to display records for delete
- 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.
Latest Tutorials
- Create Animated Skill Bar Using jQuery, HTML And CSS
- Simple And Best Responsive Web Design Using CSS Part 2
- Show Button On Hover Using HTML And CSS
- Material Design Login Form Using jQuery And CSS
- Animated Progress Bar Using jQuery And CSS
- Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
- Get Website Statistics Using PHP, jQuery And MySQL
- HTML5 Login And Signup Form With Animation Using jQuery
- Facebook Style Homepage Design Using HTML And CSS
- View Webpage In Fullscreen Using jQuery