PHP - Easy Inline Update Row Using MySQLi

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

In this tutorial we will create a Easy Inline Update Row using PHP. This code will launch a modal to update the data in the table when the user click the edit button. The code use MySQLi UPDATE query to update the targeted data by adding an id in the WHERE clause. 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_inline_update, 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_inline_update");
  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 - Easy Inline Update Row Using MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Username</th>
  26. <th>Lastname</th>
  27. <th>Username</th>
  28. <th>Password</th>
  29. <th>Action</th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <?php
  34. require 'conn.php';
  35. $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  36. while($fetch = mysqli_fetch_array($query)){
  37. ?>
  38. <tr>
  39. <td><?php echo $fetch['firstname']?></td>
  40. <td><?php echo $fetch['lastname']?></td>
  41. <td><?php echo $fetch['username']?></td>
  42. <td>************</td>
  43. <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>
  44. </tr>
  45. <?php
  46.  
  47. include 'edit.php';
  48.  
  49. }
  50. ?>
  51. </tbody>
  52. </table>
  53. </div>
  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_user.php">
  59. <div class="modal-header">
  60. <h3 class="modal-title">Add User</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" name="firstname" class="form-control" required="required"/>
  68. </div>
  69. <div class="form-group">
  70. <label>Lastname</label>
  71. <input type="text" name="lastname" class="form-control" required="required" />
  72. </div>
  73. <div class="form-group">
  74. <label>Username</label>
  75. <input type="text" name="username" class="form-control" required="required" />
  76. </div>
  77. <div class="form-group">
  78. <label>Password</label>
  79. <input type="password" name="password" max="12" class="form-control" required="required" />
  80. </div>
  81. </div>
  82. </div>
  83. <div style="clear:both;"></div>
  84. <div class="modal-footer">
  85. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  86. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  87. </div>
  88. </div>
  89. </form>
  90. </div>
  91. </div>
  92. </div>
  93. <script src="js/jquery-3.2.1.min.js"></script>
  94. <script src="js/bootstrap.js"></script>
  95. </body>
  96. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the user information 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_user.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $username = $_POST['username'];
  8. $password = $_POST['password'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$username', '$password')") 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 update the MySQLi table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
edit.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_user.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>Username</label>
  22. <input type="text" name="username" value="<?php echo $fetch['username']?>" class="form-control" required="required" />
  23. </div>
  24. <div class="form-group">
  25. <label>Password</label>
  26. <input type="text" name="password" max="12" class="form-control" required="required" />
  27. </div>
  28. </div>
  29. </div>
  30. <div style="clear:both;"></div>
  31. <div class="modal-footer">
  32. <button name="update" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button>
  33. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  34. </div>
  35. </div>
  36. </form>
  37. </div>
  38. </div>
  39. </div>

update_user.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. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10.  
  11. mysqli_query($conn, "UPDATE `user` SET `firstname` = '$firstname', `lastname` = '$lastname', `username` = '$username', `password`='$password' WHERE `user_id` = '$user_id'") or die(mysqli_error());
  12.  
  13. header("location: index.php");
  14. }
  15. ?>

There you have it we successfully created Easy Inline Update Row 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.