PHP - Simple Restore Deleted Data

In this tutorial we will create a Simple Restore Deleted Data using PHP. This code can restore a deleted data when the user click the button. The code use mysqli_query() to fetch all data in the database, therefore it can retrieve back the delete primary key by alternating the INSERT and DELETE query. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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_restore, 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_restore");
  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 shown below 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="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">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 Restore Deleted Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  18. <a href="trash.php" class="pull-right"><span class="glyphicon glyphicon-trash"></span> Trash Bin</a>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Age</th>
  26. <th>Address</th>
  27. <th>Action</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33.  
  34. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  35. while($fetch = mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['age']?></td>
  41. <td><?php echo $fetch['address']?></td>
  42. <td><a class="btn btn-danger" href="remove.php?mem_id=<?php echo $fetch['mem_id']?>"><span class="glyphicon glyphicon-remove"></span> Remove</a></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. </tbody>
  48. </table>
  49. </div>
  50. <div class="modal fade" id="form_modal" aria-hidden="true">
  51. <div class="modal-dialog">
  52. <div class="modal-content">
  53. <form method="POST" action="save_member.php">
  54. <div class="modal-header">
  55. <h3 class="modal-title">Add Member</h3>
  56. </div>
  57. <div class="modal-body">
  58. <div class="col-md-2"></div>
  59. <div class="col-md-8">
  60. <div class="form-group">
  61. <label>Firstname</label>
  62. <input type="text" name="firstname" class="form-control" required="required"/>
  63. </div>
  64. <div class="form-group">
  65. <label>Lastname</label>
  66. <input type="text" name="lastname" class="form-control" required="required"/>
  67. </div>
  68. <div class="form-group">
  69. <label>Age</label>
  70. <input type="number" min="1" name="age" class="form-control" required="required" />
  71. </div>
  72. <div class="form-group">
  73. <label>Address</label>
  74. <input type="text" name="address" class="form-control" required="required" />
  75. </div>
  76. </div>
  77. </div>
  78. <div style="clear:both;"></div>
  79. <div class="modal-footer">
  80. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  81. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  82. </div>
  83. </div>
  84. </form>
  85. </div>
  86. </div>
  87. </div>
  88. <script src="js/jquery-3.2.1.min.js"></script>
  89. <script src="js/bootstrap.js"></script>
  90. </body>
  91. </html>
trash.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="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">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 Restore Deleted Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <a href="index.php" class="pull-right">Go to main page</a>
  18. <br /><br />
  19. <div class="alert alert-danger">TRASH BIN</div>
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Age</th>
  26. <th>Address</th>
  27. <th>Action</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33.  
  34. $query = mysqli_query($conn, "SELECT * FROM `trash`") or die(mysqli_error());
  35. while($fetch = mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['age']?></td>
  41. <td><?php echo $fetch['address']?></td>
  42. <td><a class="btn btn-success" href="restore.php?trash_id=<?php echo $fetch['mem_id']?>"><span class="glyphicon glyphicon-reset"></span> Restore</a></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. </tbody>
  48. </table>
  49. </div>
  50. </body>
  51. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_member.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $age = $_POST['age'];
  8. $address = $_POST['address'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$age', '$address')") or die(mysqli_error());
  11.  
  12. header("location: index.php");
  13. }
  14. ?>

Creating Main Function

This code contains the main function of the application. This code can remove and restore the data when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as shown below. remove.php
  1. <?php
  2.  
  3. require_once 'conn.php';
  4.  
  5. if(ISSET($_REQUEST['mem_id'])){
  6. $mem_id = $_REQUEST['mem_id'];
  7. $query=mysqli_query($conn, "SELECT * FROM `member` WHERE `mem_id` = '$mem_id'") or die(mysqli_error());
  8. $fetch=mysqli_fetch_array($query);
  9.  
  10. mysqli_query($conn, "INSERT INTO `trash` VALUES('', '$fetch[mem_id]', '$fetch[firstname]', '$fetch[lastname]', '$fetch[age]', '$fetch[address]')") or die(mysqli_error());
  11. mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id` = '$mem_id'") or die(mysqli_error());
  12. header('location:index.php');
  13. }
  14.  
  15. ?>
restore.php
  1. <?php
  2.  
  3. require_once 'conn.php';
  4.  
  5. if(ISSET($_REQUEST['trash_id'])){
  6. $trash_id = $_REQUEST['trash_id'];
  7. $query=mysqli_query($conn, "SELECT * FROM `trash` WHERE `trash_id` = '$trash_id'") or die(mysqli_error());
  8. $fetch=mysqli_fetch_array($query);
  9.  
  10. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$fetch[firstname]', '$fetch[lastname]', '$fetch[age]', '$fetch[address]')") or die(mysqli_error());
  11. mysqli_query($conn, "DELETE FROM `trash` WHERE `trash_id` = '$trash_id'") or die(mysqli_error());
  12. header('location:index.php');
  13. }
  14.  
  15. ?>
There you have it we successfully created Simple Restore Deleted Data 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!

Add new comment