PHP - Display User Details

In this tutorial we will create a Display User Details using PHP. This code can display complete detail of a user in the table when user click the view detail button. The code use MySQLi SELECT query to display a specific row data in the table that binded in a button in order to view the complete details in the modal dialog. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

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_detail, 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=mysqli_connect("localhost", "root", "", "db_detail");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

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 User Details</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-4">
  18.                         <form method="POST" action="save.php">
  19.                                 <div class="form-group">
  20.                                         <label>Firstname</label>
  21.                                         <input type="text" class="form-control" name="firstname" required="required"/>
  22.                                 </div>
  23.                                 <div class="form-group">
  24.                                         <label>Lastname</label>
  25.                                         <input type="text" class="form-control" name="lastname" required="required"/>
  26.                                 </div>
  27.                                 <div class="form-group">
  28.                                         <label>Gender</label>
  29.                                         <select name="gender" class="form-control" required="required">
  30.                                                 <option value="Male">Male</option>
  31.                                                 <option value="Female">Female</option>
  32.                                         </select>
  33.                                 </div>
  34.                                 <div class="form-group">
  35.                                         <label>Age</label>
  36.                                         <input type="number" class="form-control" name="age" required="required"/>
  37.                                 </div>
  38.                                 <div class="form-group">
  39.                                         <label>Address</label>
  40.                                         <input type="text" class="form-control" name="address" required="required"/>
  41.                                 </div>
  42.                                 <div class="form-group">
  43.                                         <label>Username</label>
  44.                                         <input type="text" class="form-control" name="username" max="20" required="required"/>
  45.                                 </div>
  46.                                 <div class="form-group">
  47.                                         <label>Password</label>
  48.                                         <input type="password" class="form-control" name="password" max="12" required="required"/>
  49.                                 </div>
  50.                                 <center><button class="btn btn-primary" name="save">Save</button></center>
  51.                         </form>
  52.                 </div>
  53.                 <div class="col-md-8">
  54.                         <table class="table table-bordered">
  55.                                 <thead class="alert-info">
  56.                                         <tr>
  57.                                                 <th>Firstname</th>
  58.                                                 <th>Lastname</th>
  59.                                                 <th>Action</th>
  60.                                         </tr>
  61.                                 </thead>
  62.                                 <tbody>
  63.                                         <?php
  64.                                                 require'conn.php';
  65.                                                 $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  66.                                                 while($fetch=mysqli_fetch_array($query)){
  67.                                         ?>
  68.                                                 <tr>
  69.                                                         <td><?php echo $fetch['firstname']?></td>
  70.                                                         <td><?php echo $fetch['lastname']?></td>
  71.                                                         <td><button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal<?php echo $fetch['user_id']?>">View Details</button></td>
  72.                                                 </tr>
  73.                                                
  74.                                                 <div class="modal fade" id="form_modal<?php echo $fetch['user_id']?>" aria-hidden="true">
  75.                                                         <div class="modal-dialog">
  76.                                                                 <div class="modal-content">
  77.                                                                         <div class="modal-header">
  78.                                                                                 <h3 class="modal-title">User Details</h3>
  79.                                                                         </div>
  80.                                                                         <div class="modal-body">
  81.                                                                                 <div class="col-md-2"></div>
  82.                                                                                 <div class="col-md-8">
  83.                                                                                         <div class="col-md-6">
  84.                                                                                                 <h4>Firstname</h4>
  85.                                                                                                 <p><?php echo $fetch['firstname']?></p>
  86.                                                                                         </div>
  87.                                                                                         <div class="col-md-6">
  88.                                                                                                 <h4>Lastname</h4>
  89.                                                                                                 <p><?php echo $fetch['lastname']?></p>
  90.                                                                                         </div>
  91.                                                                                         <br style="clear:both;"/>
  92.                                                                                         <div class="col-md-6">
  93.                                                                                                 <h4>Gender</h4>
  94.                                                                                                 <p><?php echo $fetch['gender']?></p>
  95.                                                                                         </div>
  96.                                                                                         <div class="col-md-6">
  97.                                                                                                 <h4>Age</h4>
  98.                                                                                                 <p><?php echo $fetch['age']?></p>
  99.                                                                                         </div>
  100.                                                                                                 <br style="clear:both;"/>
  101.                                                                                         <div class="col-md-12">
  102.                                                                                                 <h4>Address</h4>
  103.                                                                                                 <p><?php echo $fetch['address']?></p>
  104.                                                                                         </div>
  105.                                                                                         <br style="clear:both;"/>
  106.                                                                                         <div class="col-md-6">
  107.                                                                                                 <h4>Username</h4>
  108.                                                                                                 <p><?php echo $fetch['username']?></p>
  109.                                                                                         </div>
  110.                                                                                         <div class="col-md-6">
  111.                                                                                                 <h4>Password</h4>
  112.                                                                                                 <p>************</p>
  113.                                                                                         </div>
  114.                                                                                 </div>
  115.                                                                         </div>
  116.                                                                         <div style="clear:both;"></div>
  117.                                                                         <div class="modal-footer">
  118.                                                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  119.                                                                         </div>
  120.                                                                 </div>
  121.                                                         </div>
  122.                                                 </div>
  123.                                         <?php
  124.                                                 }
  125.                                         ?>
  126.                                 </tbody>
  127.                         </table>
  128.                 </div>
  129.         </div>
  130. <script src="js/jquery-3.2.1.min.js"></script> 
  131. <script src="js/bootstrap.js"></script>
  132. </body>
  133. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2.         require_once'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $firstname=$_POST['firstname'];
  6.                 $lastname=$_POST['lastname'];
  7.                 $gender=$_POST['gender'];
  8.                 $age=$_POST['age'];
  9.                 $address=$_POST['address'];
  10.                 $username=$_POST['username'];
  11.                 $password=$_POST['password'];
  12.        
  13.                 mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$gender', '$age', '$address', '$username', '$password')") or die(mysqli_error());
  14.                 header("location: index.php");
  15.         }
  16. ?>
There you have it we successfully created Display User Details using PHP. 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!

Comments

hello

Add new comment