Skip to main content

Creating Simple Auto Archive Data in PHP

Screenshot
Body

Learn how to create Simple Auto Archive Data using PHP. An advanced PHP technique that can archive data when it reaches the due date. This code can be used to store the data that have submitted to the archive section to be able to re-read the detail of that data.

In this tutorial we will create a Simple Auto Archive Data using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by newly coders for its user-friendly environment.

So Let's do the coding...

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_archive. After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

You can also create the database tables programmatically. To do this, copy/paste the code below into your PHPMyAdmin SQL Tab of your newly created database. Then click the GO

  1. CREATE TABLE `archive` (
  2. `archive_id` int(11) NOT NULL,
  3. `product_id` int(11) NOT NULL,
  4. `product_code` varchar(50) NOT NULL,
  5. `product_name` varchar(100) NOT NULL,
  6. `description` text NOT NULL,
  7. `date_archived` varchar(20) NOT NULL
  8.  
  9. INSERT INTO `archive` (`archive_id`, `product_id`, `product_code`, `product_name`, `description`, `date_archived`) VALUES
  10. (1, 1, '6632', 'Milk', 'Milk', '2019-02-12');
  11.  
  12. CREATE TABLE `product` (
  13. `product_id` int(11) NOT NULL,
  14. `product_code` varchar(5) NOT NULL,
  15. `product_name` varchar(100) NOT NULL,
  16. `description` text NOT NULL,
  17. `due_date` varchar(20) NOT NULL
  18.  
  19. INSERT INTO `product` (`product_id`, `product_code`, `product_name`, `description`, `due_date`) VALUES
  20. (1, '8845', 'Chocolate', 'Drinks', '2019-02-15');
  21.  
  22. ALTER TABLE `archive`
  23. ADD PRIMARY KEY (`archive_id`);
  24. ALTER TABLE `product`
  25. ADD PRIMARY KEY (`product_id`);
  26. ALTER TABLE `archive`
  27. ALTER TABLE `product`

Creating the database connection

Open your any kind of text editor such as notepad++. Then just copy/paste the code below then save it as conn.php.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_archive");
  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 paste it into your text editor, then save it as shown below.

index.php
  1. <!DOCTYPE html>
  2. <?php require 'archive_query.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 Archive 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 product</button>
  19. <a href="archive.php" class="pull-right">Archive</a>
  20. <br /><br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Product Code</th>
  25. <th>Product Name</th>
  26. <th>Description</th>
  27. <th>Due Date</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33.  
  34. $query = mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error($conn));
  35. while($fetch = mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['product_code']?></td>
  39. <td><?php echo $fetch['product_name']?></td>
  40. <td><?php echo $fetch['description']?></td>
  41. <td><?php echo $fetch['due_date']?></td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. </tbody>
  47. </table>
  48. </div>
  49. <div class="modal fade" id="form_modal" aria-hidden="true">
  50. <div class="modal-dialog">
  51. <div class="modal-content">
  52. <form method="POST" action="save_product.php">
  53. <div class="modal-header">
  54. <h3 class="modal-title">Add Product</h3>
  55. </div>
  56. <div class="modal-body">
  57. <div class="col-md-2"></div>
  58. <div class="col-md-8">
  59. <div class="form-group">
  60. <label>Product Code</label>
  61. <input type="text" name="product_code" class="form-control" required="required"/>
  62. </div>
  63. <div class="form-group">
  64. <label>Product Name</label>
  65. <input type="text" name="product_name" class="form-control" required="required"/>
  66. </div>
  67. <div class="form-group">
  68. <label>Description</label>
  69. <input type="text" name="description" class="form-control" required="required" />
  70. </div>
  71. <div class="form-group">
  72. <label>Due Date</label>
  73. <input type="date" name="due_date" class="form-control" required="required" />
  74. </div>
  75. </div>
  76. </div>
  77. <div style="clear:both;"></div>
  78. <div class="modal-footer">
  79. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  80. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  81. </div>
  82. </div>
  83. </form>
  84. </div>
  85. </div>
  86. </div>
  87. <script src="js/jquery-3.2.1.min.js"></script>
  88. <script src="js/bootstrap.js"></script>
  89. </body>
  90. </html>
archive.php
  1. <!DOCTYPE html>
  2. <?php require 'archive_query.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 Archive Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <a href="index.php" class="pull-right">Main Page</a>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Product Code</th>
  24. <th>Product Name</th>
  25. <th>Description</th>
  26. <th>Date Archived</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 `archive`") or die(mysqli_error($conn));
  34. while($fetch = mysqli_fetch_array($query)){
  35. ?>
  36. <tr>
  37. <td><?php echo $fetch['product_code']?></td>
  38. <td><?php echo $fetch['product_name']?></td>
  39. <td><?php echo $fetch['description']?></td>
  40. <td><?php echo $fetch['date_archived']?></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. </body>
  49. </html>

Creating the Save 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 paste 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_code = $_POST['product_code'];
  6. $product_name = $_POST['product_name'];
  7. $description = $_POST['description'];
  8. $due_date = $_POST['due_date'];
  9.  
  10. $insert = mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_code', '$product_name', '$description', '$due_date')") or die(mysqli_error($conn));
  11. if($insert)
  12. header("location: index.php");
  13. }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will move the file to the archive section when the data reach the end date. To do that just copy and paste this block of codes inside the text editor, then save it as archive_query.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(strtotime($fetch['due_date']) < strtotime($date)){
  9. mysqli_query($conn, "INSERT INTO `archive` VALUES('', '$fetch[product_id]', '$fetch[product_code]', '$fetch[product_name]', '$fetch[description]', '$fetch[due_date]')") or die(mysqli_error($conn));
  10. mysqli_query($conn, "DELETE FROM `product` WHERE `product_id` = '$fetch[product_id]'") or die(mysqli_error($conn));
  11. }
  12. }
  13. ?>

DEMO

There you have it we successfully created Simple Auto Archive Data using PHP. I hope that this simple tutorial helps you to what you are looking for and you'll find it useful for your future PHP Projects. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Comments

Submitted byHerow it (not verified)on Mon, 03/09/2020 - 22:47

This is very useful tutorial
Submitted bysofie (not verified)on Thu, 04/08/2021 - 16:37

save_products.php had some issues with the auto increment id thingy. i changed it from: mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_code', '$product_name', '$description', '$due_date')") or die(mysqli_error()); to mysqli_query($conn, "INSERT INTO `product` VALUES(NULL, '$product_code', '$product_name', '$description', '$due_date')") or die(mysqli_error($conn)); first value was '' (empty string) but database wanted integer. it worked when i wrote "null" instead. i also changed the "or die" part. (added the $conn). great tutorial!

Add new comment