PHP - Simple Auto Delete When Expired

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