PHP - Simple Auto Delete When Expired

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

In this tutorial we will create a Simple Auto Delete When Expired using PHP. This code will automatically delete a data for you if it reach the duration of the day it was inputted in the database. The code itself use a simple MySQLi DELETE query by adding a conditional statement that contrast the inputted date and the current date to check if it is available to be deleted. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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 jquery that i used in this tutorial https://jquery.com/.

Lastly, 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, 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_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 'auto_delete.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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Auto Delete When Expired</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add product</button>
  20. <br /><br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Product Name</th>
  25. <th>Description</th>
  26. <th>Due Date</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;">
  30. <?php
  31. require 'conn.php';
  32.  
  33. $query = mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  34. while($fetch = mysqli_fetch_array($query)){
  35. ?>
  36. <tr>
  37. <td><?php echo $fetch['product_name']?></td>
  38. <td><?php echo $fetch['description']?></td>
  39. <td><?php echo $fetch['due_date']?></td>
  40. </tr>
  41. <?php
  42. }
  43. ?>
  44. </tbody>
  45. </table>
  46. </div>
  47. <div class="modal fade" id="form_modal" aria-hidden="true">
  48. <div class="modal-dialog">
  49. <div class="modal-content">
  50. <form method="POST" action="save_product.php">
  51. <div class="modal-header">
  52. <h3 class="modal-title">Add Product</h3>
  53. </div>
  54. <div class="modal-body">
  55. <div class="col-md-2"></div>
  56. <div class="col-md-8">
  57. <div class="form-group">
  58. <label>Product Name</label>
  59. <input type="text" name="product_name" class="form-control" required="required"/>
  60. </div>
  61. <div class="form-group">
  62. <label>Description</label>
  63. <input type="text" name="description" class="form-control" required="required" />
  64. </div>
  65. <div class="form-group">
  66. <label>Due Date</label>
  67. <input type="date" name="due_date" class="form-control" required="required" />
  68. </div>
  69. </div>
  70. </div>
  71. <div style="clear:both;"></div>
  72. <div class="modal-footer">
  73. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  74. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  75. </div>
  76. </div>
  77. </form>
  78. </div>
  79. </div>
  80. </div>
  81. <script src="js/jquery-3.2.1.min.js"></script>
  82. <script src="js/bootstrap.js"></script>
  83. </body>
  84. </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_product.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $product_name = $_POST['product_name'];
  6. $description = $_POST['description'];
  7. $due_date = $_POST['due_date'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_name', '$description', '$due_date')") or die(mysqli_error());
  10. header("location: index.php");
  11. }
  12. ?>

Creating the Main Function

This code contains the main function of the application. This code will automatically remove a data in the MySQLi database. To do that just copy and write this block of codes inside the text editor, then save it as auto_delete.php.

  1. <?php
  2.  
  3. require_once 'conn.php';
  4.  
  5. $query = mysqli_query($conn, "SELECT * FROM `product`");
  6. $date = date("Y-m-d");
  7. while($fetch = mysqli_fetch_array($query)){
  8. if($fetch['due_date'] === $date){
  9. mysqli_query($conn, "DELETE FROM `product` WHERE `product_id` = '$fetch[product_id]'") or die(mysqli_error());
  10. }
  11. }
  12.  
  13. ?>

There you have it we successfully created Simple Auto Delete When Expired 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.