PHP - Display Data After Submission In PDO

Submitted by: 
Visitors have accessed this post 205 times.

In this tutorial we will create a Display Data After Submission using PDO. This code will display the data after inserting it to the database server when user submit the form inputs. The code use lastInsertId() with PDO connection as a parameter to get the last id and immediately display it by adding the id in the WHERE clause. This a user-friendly program feel free to modify and use it to your system.

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_id, 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_id', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to coonect 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="container-fluid">
  10. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">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 - Display Data After Submission In PDO</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-6">
  19. <form method="POST" action="">
  20. <div class="form-group">
  21. <label>Name</label>
  22. <input type="text" name="name" class="form-control" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  31. </div>
  32. <center><button class="btn btn-primary" name="add" >Add</button></center>
  33. <br />
  35. <?php include'insert.php'?>
  37. </form>
  38. </div>
  39. <div class="col-md-6">
  40. <table class="table table-bordered">
  41. <thead class="alert-info">
  42. <tr>
  43. <th>Name</th>
  44. <th>Username</th>
  45. <th>Password</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. <?php
  50. require'conn.php';
  51. $query = $conn->prepare("SELECT * FROM `user`");
  52. $query->execute();
  53. while($row = $query->fetch()){
  54. echo "<tr>
  55. <td>".$row['name']."</td>
  56. <td>".$row['username']."</td>
  57. <td>****</td>
  58. </tr>";
  59. }
  60. ?>
  61. </tbody>
  62. </table>
  63. </div>
  64. </div>
  65. </body>
  66. </html>

Creating the Main Function

This code contains the main function of the application. This code will display the data after submission 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
  3. require_once 'conn.php';
  5. if(ISSET($_POST['add'])){
  7. $name = $_POST['name'];
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  12. try{
  13. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. $sql = "INSERT INTO `user`(name, username, password) VALUES ('$name', '$username', '$password')";
  15. $conn->exec($sql);
  16. $id=$conn->lastInsertId();
  17. $query = $conn->prepare("SELECT * FROM `user` WHERE `user_id` = '$id'");
  18. $query->execute();
  19. $row = $query->fetch();
  20. echo "<center><label>You have inserted <span class='text-danger'>".$row['name']."</span> has a new user.</label></center>";
  21. }catch(PDOException $e){
  22. echo $e->getMessage();
  23. }
  25. }
  26. ?>

There you have it we successfully created a Display Data After Submission 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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.