Delete Data Using Date in PHP Using PDO

If you are looking for Auto Delete Data Using Current Date then you are at the right place. This source code will help you on how to create a simple program that the data will automatically delete when the expiration date will appear. To set the expiration date, we have one input field to add a number of days to the current date and this step to get the expiration date to delete the data automatically. Let's start with:

Creating our Table

We are going to make our database.
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "delete_by_date".
  3. After creating a database name, then we are going to create our table. And name it as "tbl_member".
  4. Kindly copy the code below.
  1. CREATE TABLE `tbl_member` (
  2. `tbl_member_id` INT(11) NOT NULL,
  3. `first_name` VARCHAR(100) NOT NULL,
  4. `last_name` VARCHAR(100) NOT NULL,
  5. `contact_number` VARCHAR(100) NOT NULL,
  6. `email` VARCHAR(100) NOT NULL,
  7. `address` VARCHAR(100) NOT NULL,
  8. `expiration_date` VARCHAR(100) NOT NULL
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Creating Database Connection

  1. <?php
  2. $conn = new PDO('mysql:host=localhost; dbname=delete_by_date','root', '');
  3. ?>

Creating Form Field

This form field where you enter the information or data and to set the expiration date to delete the data automatically in the database table.
  1. <form method="post" action="add_person_query.php">
  2. <table border="1" cellspacing="5" cellpadding="5" width="100%">
  3. <tr>
  4. <td>
  5. <label>First Name</label>
  6. </td>
  7. <td width="395px">
  8. <input type="text" class="text_Box" name="first_name" autofocus="autofocus" placeholder="First Name ....." required />
  9. </td>
  10. </tr>
  11. <tr>
  12. <td>
  13. <label>Last Name</label>
  14. </td>
  15. <td width="395px">
  16. <input type="text" class="text_Box" name="last_name" placeholder="Last Name ....." required />
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>
  21. <label>Contact Number</label>
  22. </td>
  23. <td width="395px">
  24. <input type="text" class="text_Box" name="contact_number" placeholder="Contact Number ....." required />
  25. </td>
  26. </tr>
  27. <tr>
  28. <td>
  29. <label>Email</label>
  30. </td>
  31. <td width="395px">
  32. <input type="email" class="text_Box" name="email" placeholder="Email .....">
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <label>Address</label>
  38. </td>
  39. <td width="395px">
  40. <input type="text" class="text_Box" name="address" placeholder="Address ....." required />
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. <label>No. of Days</label>
  46. </td>
  47. <td width="395px">
  48. <input type="text" class="text_Box" name="number_of_days" placeholder="Number of Days ....." required />
  49. </td>
  50. </tr>
  51. <tr>
  52. <td colspan="2">
  53. <a>
  54. <button type="submit" class="btn_confirm">
  55. Save Data
  56. </button>
  57. </a>
  58. </td>
  59. </tr>
  60. </form>
This is the result of the code above: Result

Creating INSERT Statement Using PDO

This PHP script is used to save our data in the database table.
  1. <?php
  2. include ('db.php');
  3.  
  4. $first_name = $_POST['first_name'];
  5. $last_name = $_POST['last_name'];
  6. $contact_number = $_POST['contact_number'];
  7. $email = $_POST['email'];
  8. $address = $_POST['address'];
  9. $number_of_days = $_POST['number_of_days'];
  10. $dates = mktime(0,0,0,date("m"),date("d")+$number_of_days,date("Y"));
  11. $expiration_date = date("Y/m/d", $dates);
  12.  
  13. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. $sql = "INSERT INTO tbl_member (first_name, last_name, contact_number, email, address, expiration_date)
  15. VALUES ('$first_name', '$last_name', '$contact_number', '$email', '$address', '$expiration_date')";
  16.  
  17. $conn->exec($sql);
  18. echo "<script>alert('Successfully Added!'); window.location='index.php'</script>";
  19. ?>

Creating DELETE Statement

This PHP script for DELETE Statement is used to delete the data in the database table and we are going to use the set of expiration date and it will automatically delete when the expiration date will appear.
  1. <?php
  2. require_once('db.php');
  3.  
  4. $curdate=date("Y/m/d");
  5.  
  6. // sql to delete a record
  7. $sql = "Delete from tbl_member where expiration_date = '$curdate'";
  8.  
  9. // use exec() because no results are returned
  10. $conn->exec($sql);
  11. ?>

This our example data in the database table:

As you can see in the box, that's the expiration date of every data in the database table. It will delete automatically if the expiration date will appear. Result And, that's it. This is the steps on how to delete data automatically using a date in PHP using PDO. Kindly click the "Download Code" button below for full source code. Thank you very much. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment