PHP - Simple Auto Archive Data

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

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 a 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

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