PHP - Remove Table Row With Modal Using PDO
Submitted by razormist on Tuesday, January 7, 2020 - 09:46.
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.
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!
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.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.- <?php
- $conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_delete_modal', 'root', '');
- if(!$conn){
- }
- ?>
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Remove Table Row With Modal Using PDO</h3>
- <hr style="border-top:1px dotted #ccc;" />
- <div class="col-md-4">
- <form method="POST" action="insert.php">
- <div class="form-group">
- <label>Firstname</label>
- <input type="text" name="firstname" class="form-control" required="required"/>
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input type="text" name="lastname" class="form-control" required="required" />
- </div>
- <div class="form-group">
- <label>Address</label>
- <input type="text" name="address" class="form-control" required="required"/>
- </div>
- <center><button name="save" class="btn btn-primary">Save</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <?php
- require 'conn.php';
- $query = $conn->prepare("SELECT * FROM `member`");
- $query->execute();
- while($row = $query->fetch()){
- ?>
- <tr>
- <td><?php echo $row['firstname']?></td>
- <td><?php echo $row['lastname']?></td>
- <td><?php echo $row['address']?></td>
- <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>
- </tr>
- <div class="modal fade" id="modal<?php echo $row['mem_id']?>" aria-hidden="true">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <h3 class="modal-title">System Information</h3>
- </div>
- <div class="modal-body">
- <center><h3 class="text-danger">Are you sure you want to remove this record?</h3></center>
- </div>
- <div class="modal-footer">
- <a href="remove.php?id=<?php echo $row['mem_id']?>" class="btn btn-danger">Yes</a>
- <button class="btn btn-primary" type="button" data-dismiss="modal">No</button>
- </div>
- </div>
- </div>
- </div>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </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.- <?php
- require_once 'conn.php';
- $firstname = $_POST['firstname'];
- $lastname = $_POST['lastname'];
- $address = $_POST['address'];
- try{
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname', '$lastname', '$address')";
- }catch(PDOException $e){
- echo $e->getMessage();
- }
- $conn = null;
- }
- ?>
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.- <?php
- require_once 'conn.php';
- $query = $conn->prepare("DELETE from `member` WHERE `mem_id`='$_REQUEST[id]'");
- $query->execute();
- echo "<script>alert('Successfully remove a row!')</script>";
- echo "<script>window.location='index.php'</script>";
- }
- ?>
Add new comment
- 278 views