PHP - Simple Registration Form

In this tutorial we will create a Simple Registration Form using PHP. This code can register a user account when the input form submitted to the database server. The code itself use HTML POST method to submit the user data inputs to the MySQLi server in order to create a user account. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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_user, 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_user");
  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">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 - Simple Registration Form</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>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>Username</label>
  29.                                         <input type="text" class="form-control" name="username" required="required"/>
  30.                                 </div>
  31.                                 <div class="form-group">
  32.                                         <label>Password</label>
  33.                                         <input type="password" class="form-control" maxlength="12" name="password" required="required"/>
  34.                                 </div>
  35.                                 <p><input type="checkbox" id="toggle"/> I agree with the terms and conditions.</p>
  36.                                 <button class="btn btn-primary" id="register" name="register">Register</button>
  37.                         </form>
  38.                 </div>
  39.                 <div class="col-md-6">
  40.                 <?php include 'register.php'?>
  41.                 </div>
  42.         </div>
  43. <script>
  44. var checkbox = document.getElementById("toggle");
  45. var register = document.getElementById("register");
  46. register.disabled = true;
  47. checkbox.onchange = function(){
  48.         register.disabled = !this.checked;
  49. }
  50. </script>      
  51. </body>
  52. </html>

Creating the Main Function

This code contains the main function of the application. This code will register a new user account when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.register.php.
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         if(ISSET($_POST['register'])){
  5.                 $firstname = $_POST['firstname'];
  6.                 $lastname = $_POST['lastname'];
  7.                 $username = $_POST['username'];
  8.                 $password = $_POST['password'];
  9.                
  10.                 mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$username', '$password')") or die(mysqli_error());
  11.                 echo "<h3 class='text-success'>User account registered!</h3>";
  12.         }
  13. ?>
There you have it we successfully created Simple Registration Form 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

I tried the code and it gave me this error Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home/adroitho/public_html/reguser/conn.php on line 2 Error: Failed to connect to database!

Hi, I had the same problem, try MySQL query at your SQL server, find out about the links to your server or copy your code and use a php-analyzer, and correct the code.

Add new comment