Follow us on

banner image

Delete Multiple Records in PHP MYSQL using Checkbox

Delete Multiple Records in PHP MYSQL using Checkbox Techsawesome Digitalharjot

 In this Tutorial, I am gonna explain you How to Delete Multiple Records in PHP MYSQL using Checkbox, in this user can delete one more multiple records from the database by checking the checkboxes and then clicking on the dedicated deleted button will deleted the selected records from the database.

Working of the Script?

The Working of the script primarily focuses on the concept of PHP and MYSQL, as when user clicks on the delete button , records will be removed from the database, so the execution of the script is through the PHP.

Requirements before Running The Script?

In order to run this script, you need to setup the settings of database in constants.php, if you don't have any tables regarding it, Run the SQL file code that i have provided in db.sql with the demo code and run it in the SQL.

Lets Create Database and Tables in SQL using following:(db.sql)


CREATE TABLE IF NOT EXISTS `tbl_demo3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=101 ;
INSERT INTO `tbl_demo3` (`id`, `name`) VALUES
(9, 'demo9'),
(8, 'demo8'),
(7, 'demo7'),
(6, 'demo6'),
(5, 'demo5'),
(4, 'demo4'),
(3, 'demo3'),
(2, 'demo2'),
(1, 'demo1'),
(100, 'demo100');

2. Make sure that your checkbox name attribute is an array. You can give whatever name you want but just end it with a [] sign.
<form name="f" method="post" action="index.php">
<input type="submit" name="sub" value="Delete" onClick="javscript:return confirm('Are you sure you want to delete?');" >
<?php
$sql = "SELECT * FROM ".TABLE_DEMO." WHERE 1 ORDER BY id ASC";
$query = mysql_query($sql);
?>
<?php while($rs = mysql_fetch_object ($query)){ ?>
<tr>
<td style="text-align:center;"><input type="checkbox" name="ids[]" class="checkboxes" value="<?php echo stripslashes($rs->id); ?>" ></td>
<td style="text-align:center;"><?php echo stripslashes($rs->name); ?> </td>
</tr>
<?php } ?>
</form>

3. When you submit the form, collect all the checkbox values and implode their ids in a string with a comma(,) seperator.
<?php
if (count($_POST["ids"]) > 0 ) {
$all = implode(",", $_POST["ids"]);
}
?>

4. Now just fire a delete query with an IN clause and you are done.
<?php
$sql = "DELETE FROM ".TABLE_DEMO." WHERE 1 AND id IN($all)";
if ( @mysql_query($sql)) {
$errmsg = ("Rows has been deleted successfully");
} else {
$errmsg = ("Error while deleting.". mysql_error()); }
?>

Hope this post gives a clear view on how to delete multiple records with 
PHP and MYSQL. Share your feedback / comments below.

Download The Full Source Code Below:


Password For Zip File : 123456

Delete Multiple Records in PHP MYSQL using Checkbox Delete Multiple Records in PHP MYSQL using Checkbox Reviewed by Techs Awesome on November 29, 2020 Rating: 5

No comments:

Adsense 728x90

Powered by Blogger.