PHP - Display Data After Submission In PDO

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