PHP PDO Delete Data from Database - Web Development and Web Design Codes

Latest

Friday, April 28, 2017

PHP PDO Delete Data from Database

Delete Data using PHP Data Objects(PDO)

PHP PDO Delete Data from Database
PHP PDO Delete Data from Database


Hi guys in This Tutorial we will Learn how to Delete Data from Database using PHP Data Objects(PDO).
Very Simple and easy PDO codes to Delete Data for Beginners PDO.

What you Will Needs:

Database and files structure:
Database name: tutorial
Table name : test
create a table using below sql commands
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Insert The following Rows using Below SQL Commands
INSERT INTO `test` (`id`, `name`, `email`) VALUES
(12, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(11, 'Krishna', 'krishna@mail.com'),
(3, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(4, 'ANJAN', 'KumarANjan@gmail.com'),
(5, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(7, 'Hrithik', 'Hrithik@gmail.com'),
(8, 'Modhu Bonik', 'Modhu360@gmail.com'),
(9, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(13, 'Rupa', 'Rupa@hotmail.com'),
(14, 'ANJAN', 'Kumar@gmail.com'),
(15, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(16, 'Krishna', 'krishna@mail.com'),
(17, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(18, 'ANJAN', 'KumarANjan@gmail.com'),
(19, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(20, 'Hrithik', 'Hrithik@gmail.com'),
(21, 'Modhu Bonik', 'Modhu360@gmail.com'),
(22, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(23, 'Rupa', 'Rupa@hotmail.com'),
(24, 'ANJAN', 'Kumar@gmail.com'),
(25, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(26, 'ANJANBD60', 'Anjankumardhali6@gmail.comss'),
(27, 'Priyoshi', 'Priyoshi@gmail.com'),
(28, 'Rupom Chakma', 'Rupam@gmail.com'),
(29, 'Monisha Roy', 'Monisharoy@gmail.com');

Files Structure: create 4 files in any folder under htdocs folder.
PHP PDO Delete Data from Database
PHP PDO Delete Data from Database

1.connect.php
2.index.php
3.delete.php
4.style.css
Now copy and paste below codes to your files and save it.

connect.php codes

<?php
$host = "localhost"; //Host Name
$username = "root"; //MySQL Username.if you don't know leave 'root' by Defaults
$password = ""; //MySQL Password if Password Protected .else Leave it Blank
$db_name = "tutorial"; //Database Name

try {
 //Creating Connection between Server and Database
    $conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    // SET PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
  //Displaying Error Message with Detail.
    echo "Sorry failed to Connect: " . $e->getMessage();
    }
?>

index.php codes

Codes for index.php to displaying data form database with delete button
<?php
include('connect.php');
   ?>   
   <link rel="stylesheet" href="style.css" type="text/css"/>     
  <table>
     <tr>
         <th>ID</th>
      <th>Name</th>
      <th>Email</th>
      <th>Action</th>
   </tr>
   <?php
   $query = "SELECT * FROM test ORDER by id ASC";
        $stmt = $conn->prepare( $query );
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
        ?>
   
   <tr>
       <td><?php echo $row['id'];?></td>
    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><a href="delete.php?id=<?php echo $row['id'];?>">Delete Now</a></td>
             </tr>
   <?php
      }
   ?> 
  </table>
        

delete.php codes

When you Click on Delete button form index.php page it will Refer you to delete.php page.Now Delete.php Page will fetch id which you want to delete. then delete.php run query to find and delete the row.after successfully deleted it will redirect you to index.php page.
<?php
include('connect.php');

 $id=$_GET['id'];
    // sql to delete a record
    $sql = "DELETE FROM test WHERE id=$id";

    // use exec() because no results are returned
    $conn->exec($sql);
    header('location:index.php');
?>

style.css codes

Now we will adding some css codes to design our index.php page.
  table th,td{
 border: 1px solid blue;
 text-align:left;
}
table{
 border-collapse:collapse;
 width:50%;
}
th,td{
 padding:8px;
}
th{
 padding: 12px;
 font-weight:bold;
 font-size:20px;
 background:#b3b3ff;
}
tr:nth-child(even){background: #ccffcc}
tr:nth-child(odd){background:#f2d9e6}
  
If you Like This Articles Please Share with your Friends. Keep Visting for More Tutorial.Thank you.

No comments:

Post a Comment

Thank You for Your Comment

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