Delete Multiple records from MySQL using PHP with checkbox--codenair.com - Web Development and Web Design Codes

Latest

Tuesday, February 6, 2018

Delete Multiple records from MySQL using PHP with checkbox--codenair.com

Delete Multiple records from MySQL using PHP with checkbox

Delete Multiple records from MySQL using PHP with checkbox--codenair.com
Delete Multiple records from MySQL using PHP with checkbox--codenair.com

In this lesson we will learn 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.it's simple and very easy..
Just Follow The below Example to  perform Delete Multiple records from MySQL using PHP with checkbox

=>Database and Table:
Database Name: test
Table Name: user
SQL Codes to create user table:
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
SQL Codes to Insert rows into user

INSERT INTO `user` (`id`, `username`, `email`) VALUES
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'Priyoshi Debi', 'priya6@gmail.coma'),
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');

FILES:
1. connect.php
<?php
$conn=new mysqli('localhost','root','','test');
?>
2. index.php
In This page we will show username and email with checkbox and submit button.
<html>
<head>
<title>Delete Multiple records from MySQL using PHP with checkbox</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<div id="wrapper">
<h2 align="center">Delete Multiple rows in php Mysql</h2>
<?php if(isset($_GET['msg'])){
 echo $_GET['msg'];
}?>
<?php
if(isset($_GET['error'])){
 echo $_GET['error'];
}
?>
 
<table>
  <tr>
     <th>Action</th>
  <th>Name</th>
  <th>Email</th>
  </tr>
  <?php
  include("connect.php");
  $result=mysqli_query($conn,"SELECT*FROM user");
  while($row=$result->fetch_array()){?>
  <tr>
          <form method="POST" action="delete.php">
     <td><input type="checkbox" name="id[]" value="<?php echo $row['id'];?>"></td>
  <td><?php echo $row['username'];?></td>
  <td><?php echo $row['email'];?></td>
  </tr>
  <?php }?>
  <td id="submit"><input type="submit" name="submit" value="Delete Now"/></td>
</table>
  
  </form>
  </div>
</html>

3. delete.php
delete page to perform delete rows from database.. 

<?php
   include("connect.php");
   if(isset($_POST['id'])){
           $id=$_POST['id'];
  foreach($id as $key){
  $result=mysqli_query($conn,"DELETE FROM user WHERE id='$key'");
  }
  if($result==TRUE){
   header("location:index.php?msg=".urlencode('Your data Deleted'));
  }else{
   echo "Failed To Deleted Data";
  }
  }else{
  header("location:index.php?error=".urlencode('No id Selected'));
  }
?>


4. style.css
#wrapper{
 width:600px;
 margin:auto;
 background:#b3d9ff;
}
td,th{
 padding:5px;
 border:1px solid black;
 border-radius:4px;
}
table{
 border-collapse:collapse;
 width:600px;
 margin:auto;
 
}
input[type=checkbox]{
 width:30px;
 height:20px;
 cursor:pointer;
}
#submit{
 border:none;
}
input[type=submit]{
 background:black;
 color:white;
 padding:6px;
 border-radius:4px; 
 cursor:pointer;
}
th{
 font-size:15px;
 padding:5px;
 color:white;
 background:#0033cc;
 border-radius:4px;
}
tr:nth-child(odd){background:lightgreen}
tr:nth-child(even){background:lightblue}
That's it guys how to Delete Multiple records from MySQL using PHP with checkbox . keep visiting this blog for more codes. please leave your comments .Thank You...

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.