PHP - Simple Insert Form Data Using PDO

In this tutorial we will create a Simple Insert Form Data using PDO. This code can insert a form data to the database server with PDO query. The code use a PDO insert query to store the data inputs to database server with a safety feature for avoiding injection tools. 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 https://www.apachefriends.org/index.html. And, 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_insert_pdo, 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 = new PDO( 'mysql:host=localhost;dbname=db_insert_pdo', '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. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" 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 - Simple Insert Form Data Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <div class="col-md-1"></div>
  18. <div class="col-md-10">
  19. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  20. <br /><br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Username</th>
  27. <th>Password</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <?php
  32. require 'conn.php';
  33. $query = $conn->prepare("SELECT * FROM `user` ORDER BY `user_id` DESC");
  34. $query->execute();
  35. while($fetch = $query->fetch()){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['username']?></td>
  41. <td>******</td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. </tbody>
  47. </table>
  48. </div>
  49. </div>
  50. <div class="modal fade" id="form_modal" aria-hidden="true">
  51. <div class="modal-dialog">
  52. <div class="modal-content">
  53. <form method="POST" action="insert.php">
  54. <div class="modal-header">
  55. <h3 class="modal-title">Add User</h3>
  56. </div>
  57. <div class="modal-body">
  58. <div class="col-md-2"></div>
  59. <div class="col-md-8">
  60. <div class="form-group">
  61. <label>Firstname</label>
  62. <input class="form-control" type="text" name="firstname"/>
  63. </div>
  64. <div class="form-group">
  65. <label>Lastname</label>
  66. <input class="form-control" type="text" name="lastname"/>
  67. </div>
  68. <div class="form-group">
  69. <label>Username</label>
  70. <input class="form-control" type="text" name="username"/>
  71. </div>
  72. <div class="form-group">
  73. <label>Password</label>
  74. <input class="form-control" type="password" name="password"/>
  75. </div>
  76. </div>
  77. </div>
  78. <br style="clear:both;"/>
  79. <div class="modal-footer">
  80. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  81. <button class="btn btn-primary" name="insert"><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 the Main Function

This code contains the main function of the application. This code will store the data inputs 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 insert.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['insert'])){
  5. try{
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. $sql = "INSERT INTO `user` (`firstname`, `lastname`, `username`, `password`) VALUES ('$firstname', '$lastname', '$username', '$password')";
  12. $conn->exec($sql);
  13. }catch(PDOException $e){
  14. echo $e->getMessage();
  15. }
  16.  
  17. $conn = null;
  18.  
  19. echo "<script>alert('Successfully inserted data!')</script>";
  20. echo "<script>window.location='index.php'</script>";
  21. }
  22. ?>
There you have it we successfully created a Simple Insert Form Data 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!

Add new comment