PHP - Remove Table Row With Modal Using PDO

Submitted by: 
Language: 
Visitors have accessed this post 131 times.

In this tutorial we will create a Remove Table Row With Modal using PDO. This code will launch a modal dialog when user click the remove button in the table The code use PDO query to remove a specific data in the table row that needed some confirmation by a user to proceed to a necessary action. 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_modal, 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_modal', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to coonect to database!");
  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. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="<a href="https://sourcecodester.com"" rel="nofollow">https://sourcecodester.com"</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 - Remove Table Row With Modal Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <div class="col-md-4">
  18. <form method="POST" action="insert.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. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Address</th>
  41. <th>Action</th>
  42. </tr>
  43. </thead>
  44. <tbody>
  45. <?php
  46. require 'conn.php';
  47. $query = $conn->prepare("SELECT * FROM `member`");
  48. $query->execute();
  49. while($row = $query->fetch()){
  50. ?>
  51. <tr>
  52. <td><?php echo $row['firstname']?></td>
  53. <td><?php echo $row['lastname']?></td>
  54. <td><?php echo $row['address']?></td>
  55. <td><button class="btn btn-danger" data-toggle="modal" data-target="#modal<?php echo $row['mem_id']?>"><span class="glyphicon glyphicon-trash"></span> Remove</button></td>
  56. </tr>
  57.  
  58. <div class="modal fade" id="modal<?php echo $row['mem_id']?>" aria-hidden="true">
  59. <div class="modal-dialog">
  60. <div class="modal-content">
  61. <div class="modal-header">
  62. <h3 class="modal-title">System Information</h3>
  63. </div>
  64. <div class="modal-body">
  65. <center><h3 class="text-danger">Are you sure you want to remove this record?</h3></center>
  66. </div>
  67. <div class="modal-footer">
  68. <a href="remove.php?id=<?php echo $row['mem_id']?>" class="btn btn-danger">Yes</a>
  69. <button class="btn btn-primary" type="button" data-dismiss="modal">No</button>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74.  
  75. <?php
  76. }
  77. ?>
  78. </tbody>
  79. </table>
  80. </div>
  81. </div>
  82. <script src="js/jquery-3.2.1.min.js"></script>
  83. <script src="js/bootstrap.js"></script>
  84. </body>
  85. </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 insert.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 remove a table row when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as remove.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_REQUEST['id'])){
  5. $query = $conn->prepare("DELETE from `member` WHERE `mem_id`='$_REQUEST[id]'");
  6. $query->execute();
  7.  
  8. echo "<script>alert('Successfully remove a row!')</script>";
  9. echo "<script>window.location='index.php'</script>";
  10.  
  11. }
  12. ?>

There you have it we successfully created a Remove Table Row With Modal 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.