Automatic Delete Product From Database Based On Expiry Date

In this tutorial we will show you how to create a simple application called Automatic Delete Product From Database Based On Expiry Date. This simple application is created to delete an existing product based on the products expiry date. This application is made using PHP, MySQL and Bootstrap UI design to make more responsive to the users.

Sample Code

Tab-Archieve.php - This script is for viewing of product list.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Automated Delete Product Date Expiry</title>
  5. </head>
  6. <body>
  7. <div class="tab-pane fade in active" id="archieve">
  8. <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
  9. <thead>
  10. <?php
  11. $con = mysql_connect("localhost","root","");
  12. if (!$con)
  13. {
  14. die('Could not connect: ' . mysql_error());
  15. }
  16.  
  17. mysql_select_db("autodelete", $con);
  18. $curdate=date("Y/m/d");
  19. mysql_query("DELETE FROM prod_list WHERE expiry='$curdate'");
  20. mysql_close($con);
  21. ?>
  22. <?php
  23. $con = mysql_connect("localhost","root","");
  24. if (!$con)
  25. {
  26. die('Could not connect: ' . mysql_error());
  27. }
  28. mysql_select_db("autodelete", $con);
  29. $result = mysql_query("SELECT * FROM prod_list");
  30. echo "<div align='center'>Note: the data will automatically deleted when it reach the expiry date"."</div><br>";
  31. echo "
  32. <tr>
  33. <th>Product</th>
  34. <th>Description</th>
  35. <th>Expiry date</th>
  36. </tr>";
  37. while($row = mysql_fetch_array($result))
  38. {
  39. echo "<tr>";
  40. echo "<td><div align='center'>" . $row['product'] . "</td>";
  41. echo "<td><div align='center'>" . $row['description'] . "</td>";
  42. echo "<td><div align='center'>" . $row['expiry'] . "</td>";
  43. echo "</tr>";
  44. }
  45. echo "</table>";
  46. mysql_close($con);
  47. ?>
  48. </tbody>
  49. </table>
  50. </div>
  51. </body>
  52. </html>
Tab-Add.php - And this is for creating a new products to be added in the list.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Automated Delete Product Date Expiry</title>
  5. </head>
  6. <style>
  7. .containers{
  8. margin-right:345px;
  9. margin-left:320px;
  10. }
  11. </style>
  12. <body>
  13. <div class="tab-pane fade"id="add">
  14. <div class="containers">
  15. <p align="center" style="color:#000">Fill Up All The Details Below!</p>
  16. <form class="form-horizontal" method="POST" action="save.php">
  17. <div class="form-group">
  18. <label class="control-label col-sm-2" for="product">Product:</label>
  19. <div class="span4">
  20. <input type="text" name="product" class="form-control" id="product" placeholder="Product" required>
  21. </div>
  22. </div>
  23. <div class="form-group">
  24. <label class="control-label col-sm-2" for="description">Description:</label>
  25. <div class="span4">
  26. <input type="text" name="description" class="form-control" id="description" placeholder="Description" required>
  27. </div>
  28. </div>
  29. <div class="form-group">
  30. <label class="control-label col-sm-2" for="numdays">Number of Days:</label>
  31. <div class="span4">
  32. <input type="text" name="numdays" class="form-control" id="numdays" placeholder="Number of Days" required>
  33. </div>
  34. </div>
  35. <div class="form-group">
  36. <div class="col-sm-offset-2 col-sm-10">
  37. <button type="submit" name="submit" class="btn btn-primary">Submit</button>
  38. </div>
  39. </div>
  40. </form>
  41. </div>
  42. </div>
  43. </body>
  44. </html>
resultSave.php - This is for the query from the tab_add.php to create and save it directly to the database and set by the date expiry.
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7.  
  8. mysql_select_db("autodelete", $con);
  9. $numdays=$_POST['numdays'];
  10. $product=$_POST['product'];
  11. $description=$_POST['description'];
  12. $tomorrow = mktime(0,0,0,date("m"),date("d")+$numdays,date("Y"));
  13. $dats=date("Y/m/d", $tomorrow);
  14. mysql_query("INSERT INTO prod_list(product, description, expiry)VALUES('$product', '$description', '$dats')");
  15. header("location: index.php");
  16. ?>
Hope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Comments

Submitted byraju................ (not verified)on Mon, 12/12/2016 - 02:43

hi ,great work, you are using the variable +$numdays for add the day but can you tell me how we make a variable for +hours in mktime.

Add new comment