Skip to main content

PHP - Inline Delete Table Row In MySQLi


In this tutorial we will create a Inline Delete Table Row In MySQLi using PHP. This code can inline delete a MySQLi data when the user click the delete button.The code use href or Hypertext Referencing to direct the user to other page that is binded by an id in order to delete a data using MySQLi DELETE query and adding a WHERE clause conditional method. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

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_inline_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=mysqli_connect("localhost", "root", "", "db_inline_delete");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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. <?php require'conn.php'?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Inline Delete Table Row In MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="save.php">
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" class="form-control" name="firstname" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" class="form-control" name="lastname" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address</label>
  30. <input type="text" class="form-control" name="address" required="required"/>
  31. </div>
  32. <center><button class="btn btn-primary" name="save">Save</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <table class="table table-bordered">
  37. <thead class="alert-info">
  38. <tr>
  39. <th>Firstname</th>
  40. <th>Lastname</th>
  41. <th>Address</th>
  42. <th>Action</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php
  47. $query=mysqli_query($conn, 'SELECT * FROM `member`') or die(mysqli_error());
  48. while($fetch=mysqli_fetch_array($query)){
  49. ?>
  50. <tr>
  51. <td><?php echo $fetch['firstname']?></td>
  52. <td><?php echo $fetch['lastname']?></td>
  53. <td><?php echo $fetch['address']?></td>
  54. <td><a href="delete.php?id=<?php echo $fetch['mem_id']?>">Delete</a></td>
  55. </tr>
  56. <?php
  57. }
  58. ?>
  59. </tbody>
  60. </table>
  61. </div>
  62. </div>
  63. </body>
  64. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the data inputs to the MySQLi database server. To do that just copy and write this 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. $firstname=$_POST['firstname'];
  6. $lastname=$_POST['lastname'];
  7. $address=$_POST['address'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  10.  
  11. header('location: index.php');
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will inline delete table row when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as delete.php

  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_REQUEST['id'])){
  5. $mem_id=$_REQUEST['id'];
  6.  
  7. mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id`='$mem_id'") or die(mysqli_error());
  8. header('location: index.php');
  9. }
  10. ?>

There you have it we successfully created Inline Delete Table Row In MySQLi using PHP. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.