PHP - Simple Multiple Update Data

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

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

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Multiple Update Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="update_query.php">
  19. <?php
  20. if(ISSET($_POST['emp_id'])){
  21. ?>
  22. <button class="btn btn-warning" name="update"><span class="glyphicon glyphicon-save"></span> Update</button>
  23. <br />
  24. <?php
  25.  
  26. require 'conn.php';
  27.  
  28. $query = mysqli_query($conn, "SELECT * FROM `employee` WHERE `emp_id` IN (".implode(',',$_POST['emp_id']).")") or die(mysqli_error());
  29. while($fetch = mysqli_fetch_array($query)){
  30. ?>
  31.  
  32. <div class="col-md-4" style="border:1px SOLID #ccc; padding:10px; margin:10px 0px 10px 0px;">
  33. <div class="form-group">
  34. <label>Firstname</label>
  35. <input type="hidden" name="emp_id[]" value="<?php echo $fetch['emp_id']?>"/>
  36. <input type="text" class="form-control" value="<?php echo $fetch['firstname']?>" name="firstname[]" required="required"/>
  37. </div>
  38. <div class="form-group">
  39. <label>Lastname</label>
  40. <input type="text" class="form-control" value="<?php echo $fetch['lastname']?>" name="lastname[]" required="required"/>
  41. </div>
  42. <div class="form-group">
  43. <label>Position</label>
  44. <input type="text" class="form-control" value="<?php echo $fetch['position']?>" name="position[]" required="required"/>
  45. </div>
  46. </div>
  47. <?php
  48. }
  49. ?>
  50. </form>
  51. <?php
  52. }else{
  53. echo "<center><h2 class='text-danger'>No data selected</h2></center>";
  54. }
  55. ?>
  56. </div>
  57. </body>
  58. </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

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.