Registration Page with PHP and MySQL

In this tutorial we'll create a simple registration page using the PHP and MySQL.

Signup with PHP and MySQL


Database table

We are using users table for this tutorial.

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(80) NOT NULL,
  `name` varchar(80) NOT NULL,
  `password` varchar(80) NOT NULL


Creating a config.php file for define database and other configuration.


session_start(); /* Session Start name */

$host = "localhost"; /* Database Host name */
$user = "root"; /* Database User */
$password = ""; /* Database Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection is established or not. if not then stop execution with Connection failed error
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());


Creating a index.php file. in it create two input type field for entering username and password for registration and a button to submit the form.

<div id="custom-form-box" class="col-md-12">
 <form id="" class="form" action="" method="post">
     <h3 class="text-center text-info">Registration</h3> 
      <span class="error"><?php echo $error_msg;?></span>
      <span class="success"><?php echo $success_msg;?></span>
      <div class="form-group">
         <label for="username" class="text-info">Username:</label><br>
         <input type="text" name="username" id="username" name="userName" class="form-control">
      <div class="form-group">
         <label for="password" class="text-info">Password:</label><br>
         <input type="text" name="password" id="password" class="form-control">
      <div class="form-group submit_div">                            
         <input type="submit" name="btn_submit" class="btn btn-info btn-md" value="Register">


On the registration form submit first we check the username and password entered or not. If username and password are not empty then we check is user already registered with entered username or not.

if user already not registered with us then will perform insert query and register new user.

   // Escapes special characters in a string for use in an SQL query,
   $username = mysqli_real_escape_string($con,$_POST['username']);
   $password = mysqli_real_escape_string($con,$_POST['password']);       
   // check username and password is empty or not.
   if ($username != "" && $password != ""){        
       $sql_query = "select * from users where username='".$username."'";          
       $result = mysqli_query($con,$sql_query);           
       $UserData = mysqli_fetch_array($result);             
       // Check userdata is empty or not, if empty then create new user.
          // insert Query
         $insert_query = "INSERT INTO `users`( `username`, `password`) VALUES ('".$username."','".$password."')";          
         $result = mysqli_query($con,$insert_query);
           $success_msg = "User created.";           
           // Set error messages
          $error_msg = "User already available.";


Here, are some already registered usernames – admin, brad, larry, mark, john. please don’t try with this usernames.


In this tutorial, I showed how you can create a simple registration page with PHP and MySQL with unique username functionality.

Be First to Comment

Leave a Reply

Your email address will not be published.