Updating Data Through Bootstrap Modal Dialog Using PHP/MySQLi

In this tutorial we will create a Update Data Through Modal Dialog Using PHP/MySQLi. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. 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_modal, after that click Import then locate the database file inside the folder of the application then click ok.

tut1

You can also create a new table for "users" programatically using the code below. Paste the code in the SQL page of PHPMyAdmin.

  1. CREATE TABLE `user` (
  2. `firstname` varchar(50) NOT NULL,
  3. `lastname` varchar(50) NOT NULL,
  4. `address` varchar(100) NOT NULL

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_modal");
  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 you 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 - Update Data Through Modal Dialog Using MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-success">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Address</th>
  25. <th>Action</th>
  26. </tr>
  27. </thead>
  28. <tbody style="background-color:#fff;">
  29. <?php
  30. require 'conn.php';
  31. $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  32. while($fetch = mysqli_fetch_array($query)){
  33. ?>
  34. <tr>
  35. <td><?php echo $fetch['firstname']?></td>
  36. <td><?php echo $fetch['lastname']?></td>
  37. <td><?php echo $fetch['address']?></td>
  38. <td><button class="btn btn-warning" data-toggle="modal" type="button" data-target="#update_modal<?php echo $fetch['user_id']?>"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>
  39. </tr>
  40. <?php
  41.  
  42. include 'update_user.php';
  43.  
  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_user.php">
  53. <div class="modal-header">
  54. <h3 class="modal-title">Add User</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>Firstname</label>
  61. <input type="text" name="firstname" class="form-control" required="required"/>
  62. </div>
  63. <div class="form-group">
  64. <label>Lastname</label>
  65. <input type="text" name="lastname" class="form-control" required="required" />
  66. </div>
  67. <div class="form-group">
  68. <label>Address</label>
  69. <input type="text" name="address" class="form-control" required="required"/>
  70. </div>
  71. </div>
  72. </div>
  73. <div style="clear:both;"></div>
  74. <div class="modal-footer">
  75. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  76. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  77. </div>
  78. </div>
  79. </form>
  80. </div>
  81. </div>
  82. </div>
  83. <script src="js/jquery-3.2.1.min.js"></script>
  84. <script src="js/bootstrap.js"></script>
  85. </body>
  86. </html>

Creating PHP Query

This code contains the PHP query of the application. This code will store the data input to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_user.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  10.  
  11. header("location: index.php");
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will update the user data through the bootstrap modal. To do this just copy and write these blocks of codes as shown below inside the text editor and save it as shown below.

update_user.php
  1. <div class="modal fade" id="update_modal<?php echo $fetch['user_id']?>" aria-hidden="true">
  2. <div class="modal-dialog">
  3. <div class="modal-content">
  4. <form method="POST" action="update_query.php">
  5. <div class="modal-header">
  6. <h3 class="modal-title">Update User</h3>
  7. </div>
  8. <div class="modal-body">
  9. <div class="col-md-2"></div>
  10. <div class="col-md-8">
  11. <div class="form-group">
  12. <label>Firstname</label>
  13. <input type="hidden" name="user_id" value="<?php echo $fetch['user_id']?>"/>
  14. <input type="text" name="firstname" value="<?php echo $fetch['firstname']?>" class="form-control" required="required"/>
  15. </div>
  16. <div class="form-group">
  17. <label>Lastname</label>
  18. <input type="text" name="lastname" value="<?php echo $fetch['lastname']?>" class="form-control" required="required" />
  19. </div>
  20. <div class="form-group">
  21. <label>Address</label>
  22. <input type="text" name="address" value="<?php echo $fetch['address']?>" class="form-control" required="required"/>
  23. </div>
  24. </div>
  25. </div>
  26. <div style="clear:both;"></div>
  27. <div class="modal-footer">
  28. <button name="update" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button>
  29. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  30. </div>
  31. </div>
  32. </form>
  33. </div>
  34. </div>
  35. </div>
update_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. $user_id = $_POST['user_id'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $address = $_POST['address'];
  9.  
  10. mysqli_query($conn, "UPDATE `user` SET `firstname` = '$firstname', `lastname` = '$lastname', `address` = '$address' WHERE `user_id` = '$user_id'") or die(mysqli_error());
  11.  
  12. header("location: index.php");
  13. }
  14. ?>

There you have it we successfully created Update Data Through Modal Dialog Using MySQLi. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Comments

Submitted byMuhammad Fawad (not verified)on Thu, 02/20/2020 - 14:40

hi ,sir i am facing error that when i clicked update button of modal it does not working please help me and i am using your code in wordpress custom post type..
Submitted byKC LAI (not verified)on Wed, 03/10/2021 - 03:50

Hi I downloaded the code and run on my pc the button it doesn't work, can I know what seems to be the problem?
Submitted byLMC (not verified)on Wed, 12/21/2022 - 10:17

I don't understand why this code is not working for me. The update modal is not showing although from the video, it does show. What seems to be the problem here? I used the same code.
Submitted byAkashBanerjee (not verified)on Fri, 03/10/2023 - 22:54

Can we implement a search box here?

Add new comment