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:

<?php
  session_start();
  include('connection.php');

  if(isset($_GET['logout'])){
    session_destroy();
    unset($_SESSION['username']);
    header("location: login.php");
  }
?>
<!DOCTYPE html>
<html>
      <head>
        <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="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <meta name=viewport content="width=device-width, initial-scale=1">
      </head>
      <body>
        <div class="content">
            <form method="post" action="">
                  <a class="w7code_logo" href="admin_panel.php">
                    <img src="https://www.w7code.com/wp-content/uploads/2017/11/cropped-w7code_logo.png" />
                  </a>
                  <div class="input_data">
                    <input placeholder="Name" type="text" name="name">
                  </div>

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

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

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

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

 

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>";
      }else{
          //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'
          if(mysqli_num_rows($resultCheckUsername)>0){
            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'
          if(mysqli_num_rows($resultCheckEmail)>0){
            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');
      }
  }else{
    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.


4 comments

    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.

      Regards,
      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.

Leave a Reply

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