PHP - Simple Restore Deleted Data

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

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

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.