Skip to main content

PHP - Simple Submit POST Using PDO

Language


In this tutorial we will create a Simple Submit POST using PDO. PHP is a server-side scripting language designed primarily for web development. PDO stands for PHP Data Objects. 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_form, 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(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new PDO( 'mysql:host=localhost;dbname=db_form', 'root', '');
  3. if(!$conn){
  4. die("Fatal Error: Connection Failed!");
  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 you text editor, then save it as shown below.
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 - Simple Submit POST Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Member</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Age</th>
  25. <th>Address</th>
  26. </tr>
  27. </thead>
  28. <tbody style="background-color:#fff;">
  29. <?php
  30. require_once 'conn.php';
  31.  
  32. $sql = "SELECT * FROM `member`";
  33. $query = $conn->prepare($sql);
  34. $query->execute();
  35.  
  36. while($fetch = $query->fetch()){
  37. ?>
  38.  
  39. <tr>
  40. <td><?php echo $fetch['firstname']?></td>
  41. <td><?php echo $fetch['lastname']?></td>
  42. <td><?php echo $fetch['age']?></td>
  43. <td><?php echo $fetch['address']?></td>
  44. </tr>
  45.  
  46. <?php
  47. }
  48. ?>
  49. </tbody>
  50. </table>
  51. </div>
  52.  
  53. <div class="modal fade" id="form_modal" aria-hidden="true">
  54. <div class="modal-dialog">
  55. <div class="modal-content">
  56. <form method="POST" action="save_member.php">
  57. <div class="modal-header">
  58. <h3 class="modal-title">Add Member</h3>
  59. </div>
  60. <div class="modal-body">
  61. <div class="col-md-2"></div>
  62. <div class="col-md-8">
  63. <div class="form-group">
  64. <label>Firstname</label>
  65. <input type="text" name="firstname" class="form-control" required="required"/>
  66. </div>
  67. <div class="form-group">
  68. <label>Lastname</label>
  69. <input type="text" name="lastname" class="form-control" required="required" />
  70. </div>
  71. <div class="form-group">
  72. <label>Age</label>
  73. <input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
  74. </div>
  75. <div class="form-group">
  76. <label>Address</label>
  77. <input type="text" name="address" class="form-control" required="required"/>
  78. </div>
  79. </div>
  80. </div>
  81. <div style="clear:both;"></div>
  82. <div class="modal-footer">
  83. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  84. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  85. </div>
  86. </div>
  87. </form>
  88. </div>
  89. </div>
  90. </div>
  91.  
  92.  
  93. <script src="js/jquery-3.2.1.min.js"></script>
  94. <script src="js/bootstrap.js"></script>
  95. </body>
  96. </html>

Creating the Main Function

This code contains the main function of the application. This code will store the data inputs using PDO query. To do that write these block of codes inside the text editor and save it as save_member.php.

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

There you have it we successfully created a Simple Submit POST 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 SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

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

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