PHP Registration Form – Create Registration Form Using PHP

PHP Registration Form is the most popular part of web applications. It is commonly used to create a new account on the website with a unique email address or username. Even It stores users’ registration data in the database so that they would be able to login to the website. It is also known as a signup form.

In this tutorial, You will learn to create a simple registration form with complete validation. This form will help the user to quickly signup on to the website and store user data like Name, email, & password in the database using PHP and MYSQL. So, you can integrate it with a login system on your website.

PHP registration Form

How to Create a Registration Form Using PHP, MySQL

Before start coding, you should know the following basic points. These points will give you the best concept to create a registration form using PHP.

  • Using PHP, It can take user data and send it through the POST method.
  • Using MySQL, It can store the user data into the table of the database.
  • It does not allow invalid data and protects from hacking.
  • It creates a new account only with a unique email address.

Read Also 

PHP Form Validation

PHP Login  Form

Use the given script directly into your project. otherwise, create a folder structure like the following view for validation testing purposes.


1. Create a MySQL Database and Table

First of all, Create a MySQL database. You can also create it directly in PHPMyAdmin.

Now, Create a Table in the MySQL database using the following query.

Table Name – users

CREATE TABLE `users` (
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `created_at` timestamp(6) DEFAULT NULL,

2. Connect MySQL Database to PHP

Now, you have to connect the MYSQL database using the following script.

File Name   database.php


$hostname     = "localhost"; // enter your hostname
$username     = "root";  // enter your table username
$password     = "";   // enter your password
$databasename = "codingstatus";  // enter your database
// Create connection 
$conn = new mysqli($hostname, $username, $password,$databasename);
 // Check connection 
if ($conn->connect_error) { 
die("Unable to Connect database: " . $conn->connect_error);

3. Create a Registration Form Using HTML

Configure the following steps to create a registration form

  •  Include registration-script.php using the following script. Don’t worry it will explain it in the next step
  • Also, include the following bootstrap4 libraries to customize the form as
  •  Create a registration form using the HTML Code.

File Name – registration-form.php



<!DOCTYPE html>
<html lang="en">
  <title>PHP Registration Form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!--bootstrap4 library linked-->
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>

  <!--custom style-->
  <style type="text/css">
      background: #f7f7f7;
      padding: 20px;
      border: 1px solid orange;
      margin: 50px 0px;
    .registration-form form{
      border: 1px solid #e8e8e8;
      padding: 10px;
      background: #f3f3f3;

<div class="container-fluid">
 <div class="row">
   <div class="col-sm-4">
   <div class="col-sm-4">
    <!--====registration form====-->
    <div class="registration-form">
      <h4 class="text-center">Create a New Account</h4>
<p class="text-success text-center"><?php echo $register; ?></p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?<" method="post">

        <!--//first name//-->
        <div class="form-group">
           <label for="email">First Name</label>
<input type="text" class="form-control" placeholder="Enter First Name" name="first_name" value="<?php echo $set_firstName;?>">

           <p class="err-msg">
<?php if($fnameErr!=1){ echo $fnameErr; }?>


        <!--//Last name//-->
        <div class="form-group">
            <label for="email">Last Name</label>
<input type="text" class="form-control" placeholder="Enter Last Name" name="last_name" value="<?php echo $set_lastName;?>">

            <p class="err-msg"> 
<?php if($lnameErr!=1){ echo $lnameErr; } ?>

        <!--// Email//-->
        <div class="form-group">
            <label for="email">Email:</label>
<input type="text" class="form-control" id="email" placeholder="Enter email" name="email" value="<?php echo $set_email;?>">

            <p class="err-msg">
<?php if($emailErr!=1){ echo $emailErr; } ?>

        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control"  placeholder="Enter password" name="password"
            <p class="err-msg">
<?php if($passErr!=1){ echo $passErr; } ?>


        <!--//Confirm Password//-->
        <div class="form-group">
            <label for="pwd">Confirm Password:</label>
            <input type="password" class="form-control" placeholder="Enter Confirm password" name="cpassword">
            <p class="err-msg">
<?php if($cpassErr!=1){ echo $cpassErr; } ?>

        <button type="submit" class="btn btn-danger" name="submit">Register Now</button>
   <div class="col-sm-4">



4. Create Registration Form Script Using PHP

Before writing a script to create a registration form, you should understand the following points.

  • Include database connection file using require_once('database.php').
  • Assign a connection variable $conn to a new variable $db.
  • Check registration data are set using isset($_POST['submit']), if these are set then input data will be validated.
  • If all the input data are validated successfully, call a custom function unique_email() to check a unique registration email.
  • If the registration email is unique, call a custom function register() to insert registration input data in the MySQL database.
  • When Registration input data are inserted successfully, It will display a success message.

File Name registration-script.php


$db= $conn; // update with your database connection
// by default, error messages are empty
 // by default,set input values are empty


   //input fields are Validated with regular expression
   $validName="/^[a-zA-Z ]*$/";
   $uppercasePassword = "/(?=.*?[A-Z])/";
   $lowercasePassword = "/(?=.*?[a-z])/";
   $digitPassword = "/(?=.*?[0-9])/";
   $spacesPassword = "/^$|\s+/";
   $symbolPassword = "/(?=.*?[#?!@$%^&*-])/";
   $minEightPassword = "/.{8,}/";

 //  First Name Validation
   $fnameErr="First Name is Required"; 
else if (!preg_match($validName,$first_name)) {
   $fnameErr="Digits are not allowed";

//  Last Name Validation
   $lnameErr="Last Name is required"; 
else if (!preg_match($validName,$last_name)) {
   $lnameErr="Digit are not allowed";

//Email Address Validation
  $emailErr="Email is Required"; 
else if (!preg_match($validEmail,$email)) {
  $emailErr="Invalid Email Address";
// password validation 
  $passErr="Password is Required"; 
elseif (!preg_match($uppercasePassword,$password) || !preg_match($lowercasePassword,$password) || !preg_match($digitPassword,$password) || !preg_match($symbolPassword,$password) || !preg_match($minEightPassword,$password) || preg_match($spacesPassword,$password)) {
  $passErr="Password must be at least one uppercase letter, lowercase letter, digit, a special character with no spaces and minimum 8 length";

// form validation for confirm password
   $cpassErr="Confirm Password doest Matched";

// check all fields are valid or not
if($fnameErr==1 && $lnameErr==1 && $emailErr==1 && $passErr==1 && $cpassErr==1)

    $firstName =legal_input($first_name);
    $lastName  =legal_input($last_name);
    $email     =legal_input($email);
    $password  =legal_input(md5($password));
    // check unique email
      $register=$email." is already exist";

       // Insert data



     // set input values is empty until input field is invalid
    $set_lastName= $last_name;
    $set_email=    $email;
// check all fields are vakid or not

// convert illegal input value to ligal value formate
function legal_input($value) {
  $value = trim($value);
  $value = stripslashes($value);
  $value = htmlspecialchars($value);
  return $value;

function unique_email($email){
  global $db;
  $sql = "SELECT email FROM users WHERE email='".$email."'";
  $check = $db->query($sql);

 if ($check->num_rows > 0) {
   return true;
   return false;

// function to insert user data into database table
function register($firstName,$lastName,$email,$password){

   global $db;
   $sql="INSERT INTO users(first_name,last_name,email,password) VALUES(?,?,?,?)";
   $exec= $query->execute();
     return "You are registered successfully";
      return "Error: " . $sql . "<br>" .$db->error;


My Suggestion

Dear Developers, I hope you have learned to PHP Registration Form. Now you can easily create the registration form for your project. If you have any doubts or questions related to this tutorial, you can ask me through the below comment box. I will reply as soon as possible.

Thanks For giving time to this tutorial…

Categories PHP

3 thoughts on “PHP Registration Form – Create Registration Form Using PHP”

    • Hi john, Thanks for asking your doubt. You have asked a good question.. Read the following point. it will help you to understand..

      Its has the simple mean, if you declare extract($_POST) then you can get value of input field with its name. you will not need to get value of input field using $_POST['input_field_name'].
      suppose that you have an input file with name mobile_number like <input type="text" name="mobile_number">
      access its value as $moble_number.
      Example –
      1. With extract($_POST)
      Field Name – first_name – $first_name
      Field Name – last_name – $last_name
      Field Name – email – $email
      Field Name – password – $password
      1. Without extract($_POST)
      Field Name – first_name – $_POST['first_name']
      Field Name – last_name – $_POST['last_name']
      Field Name – email – $_POST['email']
      Field Name – password – $_POST['$password']
      I hope.. Now, Your doubt is clear with the above explanation


Leave a Comment