Create Registration form with MySQL and PHP

Create Registration form with MySQL and PHP

In membership-based website registration and login page is common.User needs to create a new account and login to the website to access services and manage its account.In this tutorial, I show how you can create a signup page with MySQL and PHP.

1. Table structure

I am using users table in the tutorial example.

CREATE TABLE `users` (
  `fname` varchar(80) NOT NULL,
  `lname` varchar(80) NOT NULL,
  `email` varchar(80) NOT NULL,
  `password` varchar(80) NOT NULL

2. Configuration

Create a new config.php file.

Completed Code

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

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());


Create a <form method='post' action='' >.

If $error_message is not empty then display $error_message value on the screen. Similarly, if $success_message is not empty then display the $success_message value on the screen.

NOTE – Value is assigned to $error_message and $success_message variable on <form > submit according to conditions. Add input fields for entering – first name, last name, email, password, and confirm password.

Also, add a submit button.

Completed Code

include "config.php";
<!DOCTYPE html>
    <title>Create Registration form with MySQL and PHP</title>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="">

    <!-- jQuery library -->
    <script src=""></script>

    <!-- Bootstrap JS -->
<script src=""></script>

    <div class='container'>
      <div class='row'>

        <div class='col-md-6' >

          <form method='post' action=''>

            // Display Error message
            <div class="alert alert-danger">
              <strong>Error!</strong> <?= $error_message ?>


            // Display Success message
            <div class="alert alert-success">
              <strong>Success!</strong> <?= $success_message ?>


            <div class="form-group">
              <label for="fname">First Name:</label>
              <input type="text" class="form-control" name="fname" id="fname" required="required" maxlength="80">
            <div class="form-group">
              <label for="lname">Last Name:</label>
              <input type="text" class="form-control" name="lname" id="lname" required="required" maxlength="80">
            <div class="form-group">
              <label for="email">Email address:</label>
              <input type="email" class="form-control" name="email" id="email" required="required" maxlength="80">
            <div class="form-group">
              <label for="password">Password:</label>
              <input type="password" class="form-control" name="password" id="password" required="required" maxlength="80">
            <div class="form-group">
              <label for="pwd">Confirm Password:</label>
              <input type="password" class="form-control" name="confirmpassword" id="confirmpassword" onkeyup='' required="required" maxlength="80">

            <button type="submit" name="btnsignup" class="btn btn-default">Submit</button>


4. Form Submit

Add following code in <head> section.

On <form > submit assign $_POST values in variables.

Validate the values –

To check the input values are valid or not created a $isValid = true variable. If any validation is false then assign false to $isValid and record not inserted.

  1. First, check if all values are entered or not. If not entered then assign false to $isValid and "Please fill all fields." to $error_message.
  2. Check if entered password and confirm password are equal or not. If not equal then assign false to $isValid and "Confirm password not matching." to $error_message.
  3. Check if $email variable value has valid email or not. If not valid then assign false to $isValid and "Invalid Email-ID." to $error_message.
  4. Check if email-id already exists in users table or not. If available then assign false to $isValid and "Email-ID is already existed." to $error_message.

If $isValid has true value then insert a new record in the users table and assign "Account created successfully." to $success_message.

Completed Code

$error_message = "";$success_message = "";

// Register user
   $fname = trim($_POST['fname']);
   $lname = trim($_POST['lname']);
   $email = trim($_POST['email']);
   $password = trim($_POST['password']);
   $confirmpassword = trim($_POST['confirmpassword']);

   $isValid = true;

   // Check fields are empty or not
   if($fname == '' || $lname == '' || $email == '' || $password == '' || $confirmpassword == ''){
     $isValid = false;
     $error_message = "Please fill all fields.";

   // Check if confirm password matching or not
   if($isValid && ($password != $confirmpassword) ){
     $isValid = false;
     $error_message = "Confirm password not matching";

   // Check if Email-ID is valid or not
   if ($isValid && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
     $isValid = false;
     $error_message = "Invalid Email-ID.";


     // Check if Email-ID already exists
     $stmt = $con->prepare("SELECT * FROM users WHERE email = ?");
     $stmt->bind_param("s", $email);
     $result = $stmt->get_result();
     if($result->num_rows > 0){
       $isValid = false;
       $error_message = "Email-ID is already existed.";


   // Insert records
     $insertSQL = "INSERT INTO users(fname,lname,email,password ) values(?,?,?,?)";
     $stmt = $con->prepare($insertSQL);

     $success_message = "Account created successfully.";

5. Demo

6. Conclusion

In this tutorial, I only cover the registration system and if you want to know how to create login page then you can view the following tutorial.

Laravel Repository Pattern Implementation

Laravel 6 Release New Features and Upgrade

Instructions to Create your first Laravel package

Upgrading Laravel To 6.0 From 5.8

Laravel Custom Casts Package

Top 12 Array Functions In PHP

Putting a Laravel App into Production

Why we use Laravel & Wink

php mysql

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Country State City Dropdown list in PHP MySQL PHP

Country state city dropdown using ajax in php. You'll learn how to populate country city state dropdown based on previous selection in php using ajax

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Ajax Live Data Search using jQuery PHP MySQL

ajax php live search with mysql database example. Here you will learn how to implement ajax live data search using php mysql database with example

Category Subcategory Dropdown in PHP MySQL Ajax

PHP mysql dynamic dropdown list onchange. Here, we'll show how to populate category and subcategory in dropdown list using ajax in PHP mysql

PHP Code for Update Data in MySQL Database - Tuts Make

PHP code for updating data in mysql database. Here we'll show you how to fetch and update form data into mysql database using PHP code. &demo