PHP - Simple Inline Delete Using PDO

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

In this tutorial we will create a Simple Inline Delete using PDO. This code can delete a data in the database server through PDO query. The code use a PDO to remove a specific data in the table row that need to confirmation by the user to proceed the necessary action.

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_delete_pdo, 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_delete_pdo', '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 - Simple Inline Delete Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. <th>Action</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <?php
  28. require 'conn.php';
  29. $query = $conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC");
  30. $query->execute();
  31. while($row = $query->fetch()){
  32. ?>
  33. <tr>
  34. <td><?php echo $row['firstname']?></td>
  35. <td><?php echo $row['lastname']?></td>
  36. <td><?php echo $row['address']?></td>
  37. <td><a class="btn btn-danger" href="delete.php?id=<?php echo $row['mem_id']?>"><span class="glyphicon glyphicon-trash "></span> Delete</a></td>
  38. </tr>
  39. <?php
  40. }
  41. ?>
  42. </tbody>
  43. </table>
  44. </div>
  45.  
  46. </body>
  47. </html>

Creating the Main Function

This code contains the main function of the application. This code will delete a 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. if(ISSET($_GET['id'])){
  3. require_once 'conn.php';
  4. $id = $_GET['id'];
  5. $query = $conn->prepare("DELETE from `member` WHERE `mem_id`='$id'");
  6. $query->execute();
  7.  
  8. echo "<script>alert('Successfully deleted a member!')</script>";
  9. echo "<script>window.location='index.php'</script>";
  10. }
  11. ?>

There you have it we successfully created a Simple Inline Delete 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.