PHP - Delete All Rows Using PDO

In this tutorial we will create a Delete All Rows using PDO. This code can delete the entire data in the database server through PDO query. The code use a PDO to instantly delete all data in the table row. This a friendly-user kind of system, feel free to use and modify it We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_pdo_delete, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_delete', 'root', '');
  3. if(!$conn){
  4. die("Fatal Error: Connection Failed!");
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="cotaniner-fluid">
  10. <a class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Delete All Rows Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form method="POST" action="save.php">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" name="firstname" class="form-control" required="required"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" name="lastname" class="form-control" required="required" />
  26. </div>
  27. <div class="form-group">
  28. <label>Address</label>
  29. <input type="text" name="address" class="form-control" required="required"/>
  30. </div>
  31. <center><button name="save" class="btn btn-primary">Save</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <button class="btn btn-danger" data-toggle="modal" data-target="#modal">Delete All</button>
  36. <br /><br />
  37. <table class="table table-bordered">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Firstname</th>
  41. <th>Lastname</th>
  42. <th>Address</th>
  43. </tr>
  44. </thead>
  45. <tbody style="background-color:#fff;">
  46. <?php
  47. require_once 'conn.php';
  48.  
  49. $sql = "SELECT * FROM `member`";
  50. $query = $conn->prepare($sql);
  51. $query->execute();
  52.  
  53. while($fetch = $query->fetch()){
  54. ?>
  55.  
  56. <tr>
  57. <td><?php echo $fetch['firstname']?></td>
  58. <td><?php echo $fetch['lastname']?></td>
  59. <td><?php echo $fetch['address']?></td>
  60. </tr>
  61.  
  62. <?php
  63. }
  64. ?>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. <div class="modal fade" id="modal" aria-hidden="true">
  70. <div class="modal-dialog">
  71. <div class="modal-content">
  72. <div class="modal-header">
  73. <h3 class="modal-title">System Information</h3>
  74. </div>
  75. <div class="modal-body">
  76. <center><h3 class="text-danger">Are you sure you want to delete all records?</h3></center>
  77. </div>
  78. <div class="modal-footer">
  79. <a href="delete.php" class="btn btn-danger">Yes</a>
  80. <button class="btn btn-primary" type="button" data-dismiss="modal">No</button>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. <script src="js/jquery-3.2.1.min.js"></script>
  86. <script src="js/bootstrap.js"></script>
  87. </body>
  88. </html>

Creating the PDO Query

This code contains the PDO query of the application. This code will store the form inputs to the database server. To do this just kindly write these block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5.  
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $address = $_POST['address'];
  9.  
  10.  
  11. try{
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname', '$lastname', '$address')";
  14. $conn->exec($sql);
  15. }catch(PDOException $e){
  16. echo $e->getMessage();
  17. }
  18.  
  19. $conn = null;
  20.  
  21. header("location: index.php");
  22.  
  23. }
  24. ?>

Creating the Main Function

This code contains the main function of the application. This code will delete all data when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as delete.php.
  1. <?php
  2. require_once 'conn.php';
  3. $query = $conn->prepare("DELETE from `member`");
  4. $query->execute();
  5.  
  6. echo "<script>alert('Successfully deleted all members!')</script>";
  7. echo "<script>window.location='index.php'</script>";
  8. ?>
There you have it we successfully created a Delete All Rows using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment