PHP - Simple Multiple Update Data

In this tutorial we will create a Simple Multiple Update Data using PHP. This code can update multiple data in just one transaction. The code use php array to handle multiple of data to be process. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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_multiple, 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_multiple");
  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. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Multiple Update Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Employee</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <form method="POST" action="update.php">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>#</th>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Position</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <?php
  31. require 'conn.php';
  32.  
  33. $query = mysqli_query($conn, "SELECT * FROM `employee`") or die(mysqli_error());
  34. while($fetch = mysqli_fetch_array($query)){
  35. ?>
  36. <tr>
  37. <td><input type="checkbox" name="emp_id[]" value="<?php echo $fetch['emp_id']?>"></td>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['position']?></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. <tfoot>
  47. <tr>
  48. <td colspan="4"><button class="btn btn-warning pull-right" name="edit"><span class="glyphicon glyphicon-edit"></span> Update</button></td>
  49. </tr>
  50. </tfoot>
  51. </form>
  52. </table>
  53. </div>
  54. <div class="modal fade" id="form_modal" aria-hidden="true">
  55. <div class="modal-dialog">
  56. <div class="modal-content">
  57. <form method="POST" action="save.php">
  58. <div class="modal-header">
  59. <h3 class="modal-title">Add Employee</h3>
  60. </div>
  61. <div class="modal-body">
  62. <div class="col-md-2"></div>
  63. <div class="col-md-8">
  64. <div class="form-group">
  65. <label>Firstname</label>
  66. <input type="text" class="form-control" name="firstname" required="required" />
  67. </div>
  68. <div class="form-group">
  69. <label>Lastname</label>
  70. <input type="text" class="form-control" name="lastname" required="required" />
  71. </div>
  72. <div class="form-group">
  73. <label>Position</label>
  74. <input type="text" class="form-control" name="position" required="required" />
  75. </div>
  76. </div>
  77. </div>
  78. <br style="clear:both;"/>
  79. <div class="modal-footer">
  80. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  81. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  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>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the MySQLi database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $position = $_POST['position'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `employee` VALUES('', '$firstname', '$lastname', '$position')") 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 update multiple data in the table. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. update.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Multiple Update Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="update_query.php">
  18. <?php
  19. if(ISSET($_POST['emp_id'])){
  20. ?>
  21. <button class="btn btn-warning" name="update"><span class="glyphicon glyphicon-save"></span> Update</button>
  22. <br />
  23. <?php
  24.  
  25. require 'conn.php';
  26.  
  27. $query = mysqli_query($conn, "SELECT * FROM `employee` WHERE `emp_id` IN (".implode(',',$_POST['emp_id']).")") or die(mysqli_error());
  28. while($fetch = mysqli_fetch_array($query)){
  29. ?>
  30.  
  31. <div class="col-md-4" style="border:1px SOLID #ccc; padding:10px; margin:10px 0px 10px 0px;">
  32. <div class="form-group">
  33. <label>Firstname</label>
  34. <input type="hidden" name="emp_id[]" value="<?php echo $fetch['emp_id']?>"/>
  35. <input type="text" class="form-control" value="<?php echo $fetch['firstname']?>" name="firstname[]" required="required"/>
  36. </div>
  37. <div class="form-group">
  38. <label>Lastname</label>
  39. <input type="text" class="form-control" value="<?php echo $fetch['lastname']?>" name="lastname[]" required="required"/>
  40. </div>
  41. <div class="form-group">
  42. <label>Position</label>
  43. <input type="text" class="form-control" value="<?php echo $fetch['position']?>" name="position[]" required="required"/>
  44. </div>
  45. </div>
  46. <?php
  47. }
  48. ?>
  49. </form>
  50. <?php
  51. }else{
  52. echo "<center><h2 class='text-danger'>No data selected</h2></center>";
  53. }
  54. ?>
  55. </div>
  56. </body>
  57. </html>
update_query.php
  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['update'])){
  4. foreach($_POST['emp_id'] as $key => $value){
  5. $emp_id = $value;
  6. $firstname = $_POST['firstname'][$key];
  7. $lastname = $_POST['lastname'][$key];
  8. $position = $_POST['position'][$key];
  9.  
  10. mysqli_query($conn, "UPDATE `employee` SET `firstname` = '$firstname', `lastname` = '$lastname', `position` = '$position' WHERE `emp_id` = '$emp_id'") or die(mysqli_error());
  11. header("location: index.php");
  12. }
  13. }
  14. ?>
There you have it we successfully created Simple Multiple Update 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