How Create a Registration Form in PHP

Today we will learn how to create a registration form in PHP. Create a registration form is a simple process, but take some time. After we have created a Login Form in PHP, we now need to create a registration form that allows users to register. So that way they can access the user panel.


In the previous class we have created five files:

  • login.php;
  • bd.php;
  • connection.php;
  • style.css;
  • user_panel.php.

Now we just need to add one more file named ‘ register.php ‘, that will contain:


    header("location: login.php");
<!DOCTYPE html>
        <title>W7Code - Register</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src=""></script>
        <meta name=viewport content="width=device-width, initial-scale=1">
        <div class="content">
            <form method="post" action="">
                  <a class="w7code_logo" href="admin_panel.php">
                    <img src="" />
                  <div class="input_data">
                    <input placeholder="Name" type="text" name="name">

                  <div class="input_data">
                    <input placeholder="Username" type="text" name="username">

                  <div class="input_data">
                    <input placeholder="Email" type="email" name="email">

                  <div class="input_data">
                    <input placeholder="Password" type="password" name="password_1">

                  <div class="input_data">
                    <input placeholder="Confirm Password"type="password" name="password_2">
                  <div class="input_data">
                    <button type="submit" class="button button1" name="register_user">Register</button>


Then we just need to enable our form, for that we will use the ‘ connection.php ‘ file that we have create in the previous class. And post there this piece of code:

//is user isset button register_user
if (isset($_POST['register_user'])) {
  //receive all input values from the form and escape them
  $name = mysqli_real_escape_string($db, $_POST['name']);
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

  //check if individually if all fields aren't empty
  if (empty($name)) { echo "Name Required!<br>"; }
  if (empty($username)) { echo "Username Required!<br>"; }
  if (empty($email)) { echo "Email Required!"; }
  if (empty($password_1)) { echo "Password Required!<br>"; }
  //check if all fields are not empty
  //if to continue the code
  //else print 'All field required'
  if(!empty($name) && !empty($email) && !empty($username) && !empty($password_1) &&!empty($password_2)){
      //check if passords matchs
      //if true continue doing the code
      //else print 'Two password not match'
      if ($password_1 != $password_2) {
          echo "The two passwords do not match<br>";
          //Check if username exists in database
          $checkUsername = "SELECT username FROM users WHERE username='$username'";
          $resultCheckUsername = mysqli_query($db, $checkUsername);
          //if mysqli_num_rows is bigger then 0
          //it means that user already exists
          //and print 'Username already exists'
            echo "Username already exists<br>";

          //Check if email exists in database
          $checkEmail = "SELECT email FROM users WHERE email='$email'";
          $resultCheckEmail = mysqli_query($db, $checkEmail);
          //if mysqli_num_rows is bigger then 0
          //it means that email already exists
          //and print 'Email already exists'
            echo "Email already exists<br>";

          //encrypt the password before saving in the database
          $password = md5($password_1);
          //insert into database the new user information
          $query = "INSERT INTO users (name, username, email, password) VALUES ('$name', '$username', '$email', '$password')";
          mysqli_query($db, $query);
          //then redirect user to login.php
          header('location: login.php');
    echo "All fields are required!<br>";


This is our final result:

Create a registration form in php is a very simple process!


Learn now:



Have any doubt? Please feel free to ask us using comments bellow.


    1. Hi Jhony Jackson,
      You can subscribe to our newsletter, and get all the blog updates. The subscription form is in the left sidebar of blog.

      W7Code Team

  1. Simply want to say your article is as astounding. The clarity
    on your put up is simply nice and that i can assume you’re an expert in this subject.

    Fine together with your permission let me to grab your RSS feed to stay updated with drawing close post.
    Thank you a million and please continue the rewarding work.

  2. It’s a pity you don’t have a donate button! I’d without a doubt donate to
    this fantastic blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to fresh updates and will share this
    site with my Facebook group. Talk soon!

    1. Hi Johny Jackson, thanks for sharing your great opinion! We have have a donation button in the right sidebar if you want donate us!
      W7code Team

Leave a Reply

Your email address will not be published. Required fields are marked *