PHP - Display User Details

Submitted by: 
Language: 
Visitors have accessed this post 431 times.

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


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