Skip to main content

PHP - Store Form Data With Modal Using PDO

In this tutorial we will create a Store Form Data With Modal using PDO. This code can store a form data to the database server using PDO when submitting. The code use PDO INSERT query to store the form inputs to database server that secure data process to prevent injection tools. This is a free program feel free to modify it or use it to your application.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_pdo_store, after that click Import then locate the database file inside the folder of the application then click ok.

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=new PDO( 'mysql:host=localhost;dbname=db_pdo_store', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to connect to database!");
  5. }
  6. ?>

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="cotaniner-fluid">
  10. <a class="navbar-brand">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 - Store Form Data With Modal Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  20. <button class="btn btn-success" 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>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Age</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <?php
  32. require_once 'conn.php';
  34. $sql = "SELECT * FROM `user`";
  35. $query = $conn->prepare($sql);
  36. $query->execute();
  38. while($fetch = $query->fetch()){
  39. ?>
  41. <tr>
  42. <td><?php echo $fetch['firstname']?></td>
  43. <td><?php echo $fetch['lastname']?></td>
  44. <td><?php echo $fetch['age']?></td>
  45. </tr>
  47. <?php
  48. }
  49. ?>
  50. </tbody>
  51. </table>
  52. </div>
  53. </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>Age</label>
  75. <input type="number" name="age" class="form-control" min="0" max="200" required="required" />
  76. </div>
  77. </div>
  78. </div>
  79. <div style="clear:both;"></div>
  80. <div class="modal-footer">
  81. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  82. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  83. </div>
  84. </div>
  85. </form>
  86. </div>
  87. </div>
  88. </div>
  91. <script src="js/jquery-3.2.1.min.js"></script>
  92. <script src="js/bootstrap.js"></script>
  93. </body>
  94. </html>

Creating the Main Function

This code contains the main function of the application. This code will store the form data to the database server when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as save_user.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['save'])){
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $age = $_POST['age'];
  11. try{
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO `user`(firstname, lastname, age) VALUES ('$firstname', '$lastname', '$age')";
  14. $conn->exec($sql);
  15. }catch(PDOException $e){
  16. echo $e->getMessage();
  17. }
  19. $conn = null;
  21. header("location: index.php");
  23. }
  24. ?>

There you have it we successfully created a Store Form Data With Modal using PDO. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.