User Registration Form Using PHP

If you are looking for the User Registration Form Using PHP then you are at the right place. In this article, we are going to learn on how to create simple user registration form using PHP Language. In the example below, you have to input the data of the user in each field to save it into a database. Then, the alert message will show "Successfully Registered" at the top of the form field. Registration Form Field - HTML This HTML source code for the user registration.
  1. <form method="post" action="">
  2.  
  3. <table border="0" width="500" align="center" class="demo-table">
  4.         <div class="alert_message"><?php if(isset($alert_message)) echo $alert_message; ?></div>
  5.        
  6.         <tr>
  7.                 <td class="text_label">Username</td>
  8.                 <td><input type="text" class="table_container" autofocus="autofocus" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>
  9.         </tr>
  10.         <tr>
  11.                 <td class="text_label">First Name</td>
  12.                 <td><input type="text" class="table_container" autofocus="autofocus" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td>
  13.         </tr>
  14.         <tr>
  15.                 <td class="text_label">Last Name</td>
  16.                 <td><input type="text" class="table_container" autofocus="autofocus" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td>
  17.         </tr>
  18.         <tr>
  19.                 <td class="text_label">Password</td>
  20.                 <td><input type="password" class="table_container" autofocus="autofocus" name="password" value=""></td>
  21.         </tr>
  22.         <tr>
  23.                 <td class="text_label">Confirm Password</td>
  24.                 <td><input type="password" class="table_container" autofocus="autofocus" name="confirm_password" value=""></td>
  25.         </tr>
  26.         <tr>
  27.                 <td class="text_label">Email</td>
  28.                 <td><input type="text" class="table_container" autofocus="autofocus" name="userEmail" value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>"></td>
  29.         </tr>
  30.         <tr>
  31.                 <td class="text_label">Gender</td>
  32.                 <td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php  } ?>> Male
  33.                 <input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php  } ?>> Female
  34.         </td>
  35.         </tr>
  36.         <tr>
  37.                 <td></td>
  38.                 <td><input type="checkbox" name="terms"> I accept Terms and Conditions</td>
  39.         </tr>
  40.  
  41. <div>
  42. <input type="submit" name="submit" value="Add User" class="btnRegister">
  43. </div>
  44.  
  45. </form>
Data Table This is the data where the user information shows in this table.
  1. <table border="1" class="table_data" cellspacing="5" cellpadding="5">
  2.         <tr style="color:blue;">
  3.  
  4.                 <th>
  5.                         User Name
  6.                 </th>
  7.                 <th>
  8.                         First Name
  9.                 </th>
  10.                 <th>
  11.                         Last Name
  12.                 </th>
  13.                 <th>
  14.                         Password
  15.                 </th>
  16.                 <th>
  17.                         Email
  18.                 </th>
  19.         </tr>
  20. <?php
  21. $conn = mysql_connect("localhost","root","");
  22. mysql_select_db("user_registration_1",$conn);
  23. $result= mysql_query("select * from registered_users order by id DESC ") or die (mysql_error());
  24. while ($row= mysql_fetch_array ($result) ){
  25. $id=$row['id'];
  26. ?>
  27.         <tr style="text-align:center; color:blue;">
  28.                 <td style="width:200px;">
  29.                         <?php echo $row['user_name']; ?>
  30.                 </td>
  31.                 <td style="width:200px; color:red;">
  32.                         <?php echo $row['first_name']; ?>
  33.                 </td>
  34.                 <td style="width:200px; color:blue;">
  35.                         <?php echo $row['last_name']; ?>
  36.                 </td>
  37.                 <td style="width:200px; color:red;">
  38.                         <?php echo $row['password']; ?>
  39.                 </td>
  40.                 <td style="width:200px; color:blue;">
  41.                         <?php echo $row['email']; ?>
  42.                 </td>
  43.         </tr>
  44. <?php } ?>
  45. </table>
PHP Query This query is used to save the user data into the database.
  1.                 require_once("db.php");
  2.                 $db_handle = new DB();
  3.                 $query = "INSERT INTO registered_users (user_name, first_name, last_name, password, email, gender) VALUES
  4.                 ('" . $_POST["userName"] . "', '" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . ($_POST["password"]) . "', '" . $_POST["userEmail"] . "', '" . $_POST["gender"] . "')";
  5.                 $result = $db_handle->insertQuery($query);
  6.                 if(!empty($result)) {
  7.                         $alert_message = "Successfully Registered!";   
  8.                         unset($_POST);
  9.                 } else {
  10.                         $alert_message = "Incorrect Details. Try Again!";      
  11.                 }
And, this is the style.
  1. <style type="text/css">
  2. body {
  3.         width:600px;
  4.         margin:auto;
  5. }
  6. .alert_message {
  7.     color: #FF0000;
  8.     font-weight: bold;
  9.     margin-left: 30px;
  10.     margin-top: 10px;
  11. }
  12. .demo-table {
  13.         border:blue 2px solid;
  14.         color:blue;
  15.         margin:20px;
  16. }
  17. .demo-table td {
  18.         padding:5px;
  19. }
  20. .table_container {
  21.         font-size:18px;
  22.         border:blue 1px solid;
  23.         border-radius: 4px;
  24.         text-indent:5px;
  25. }
  26. .btnRegister {
  27.         background-color:azure;
  28.         border:blue 1px solid;
  29.         color: blue;
  30.         width:100px;
  31.         padding:10px;
  32.         font-size:20px;
  33.         margin-left:22px;
  34.         border-radius:8px;
  35.         cursor: pointer;
  36. }
  37. .btnRegister:hover {
  38.         background:azure;
  39.         border:red 1px solid;
  40.         color: red;
  41.         width:100px;
  42.         padding:10px;
  43.         font-size:20px;
  44.         margin-left:22px;
  45.         border-radius:8px;
  46.         cursor: pointer;
  47. }
  48. .text_label {
  49.         font-size:20px;
  50.         font-weight:bold;
  51.         color:blue;
  52.         font-family:Helvitica;
  53.         text-align:center;
  54. }
  55. .table_data {
  56.     width: 86%;
  57.         margin:10px;
  58. }
  59. </style>

Output:

This is to add user data. Add User Data After saving the user data in the database. Show User Data Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment