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` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `fname` varchar(80) NOT NULL,
  `lname` varchar(80) NOT NULL,
  `email` varchar(80) NOT NULL,
  `password` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a new config.php file.

Completed Code

<?php
session_start();
$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());
}

3. HTML & PHP

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

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

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

  </head>
  <body>
    <div class='container'>
      <div class='row'>

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

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

            <h1>SignUp</h1>
            <?php 
            // Display Error message
            if(!empty($error_message)){
            ?>
            <div class="alert alert-danger">
              <strong>Error!</strong> <?= $error_message ?>
            </div>

            <?php
            }
            ?>

            <?php 
            // Display Success message
            if(!empty($success_message)){
            ?>
            <div class="alert alert-success">
              <strong>Success!</strong> <?= $success_message ?>
            </div>

            <?php
            }
            ?>

            <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>
            <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>
            <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>
            <div class="form-group">
              <label for="password">Password:</label>
              <input type="password" class="form-control" name="password" id="password" required="required" maxlength="80">
            </div>
            <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">
            </div>

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

     </div>
    </div>
  </body>
</html>

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

<?php 
$error_message = "";$success_message = "";

// Register user
if(isset($_POST['btnsignup'])){
   $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.";
   }

   if($isValid){

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

   }

   // Insert records
   if($isValid){
     $insertSQL = "INSERT INTO users(fname,lname,email,password ) values(?,?,?,?)";
     $stmt = $con->prepare($insertSQL);
     $stmt->bind_param("ssss",$fname,$lname,$email,$password);
     $stmt->execute();
     $stmt->close();

     $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.

Recommended Reading

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 Projects

PhP MySQL Projects

Simpliv LLC, a platform for learning and teaching online courses. We basically focus on online learning which helps to learn business concepts, software technology to develop personal and professional goals through video library by recognized industry experts and trainers.

Description
PHP Projects in Urdu is a comprehensive course. It has five PHP projects in Urdu. Each project is created from scratch. You will get source code of each project as well. The list of 5 projects is as under:

Project 1: Find and Replace Application
Project 2: Building an Email Marketing Application
Project 3: Creating a Contact Form
Project 4: Building a Search Engine For Database
Project 5: Building a PHP Quiz Application
As this is an intermediate level course so it has some requisites as well

This is a Urdu/Hindi video course

Who this Course is for:

Anyone who wants to learn Web development
Who wants to become a PHP Developer
Basic knowledge
You must have basic knowledge about PHP and Mysql
You must be able to write HTML and CSS as well
What will you learn
How to Find and Replace Application
How to Build an Email Marketing Application
How to Create a Contact Form
How to Build a Search Engine For Database
How to Build a PHP Quiz Application
To continue:

The Complete PHP MySQL Professional Course with 5 Projects

The Complete PHP MySQL Professional Course with 5 Projects

The Complete PHP MySQL Professional Course with 5 Projects

Description
Have you ever wonder why their are so many PHP Mysql Courses but they offer very little practical skills. Sometimes its difficult for beginners to understand the long project right after learning basics due to lack of practice.Furthermore, big courses just give presentation to projects they never ever let students see their actual content. For example the basic videos, Free starter content etc. However this course is Scam-Free course. Students can watch more than 2 hour of initial content free of cost to decide weather to buy this course or not.

This course is designed in more logical way from easy to complex modules step by step. Here is description of the course

Road Map:

Building Blocks Fundamentals 5 Hour 30 mints of Content

Projects:

Regular Expressions 25 mints
Complete Contact Form + Email 1 Hour of Content
Dynamic Web Pages (Directory Project) 1 Hour of Content
CMS Based Employee Management CRUD 2 Hour + 30 minutes of Content
CMS + Admin Panel with Stunning blog + FREE Bootstrap 4.2.1 12 Hour of Content
This course will focus on all the Fundamentals , Building blocks and Advance Concepts of PHP in complete details which are essentials for beginners. In this Course students will build project after every section to see the real world representation of PHP.

Bonuses:

Getting Started with Bootstrap 4 FrameWork from scratch without using any Bootstrap Template
Latest Bootstrap 4.2.1 Essentials Practically in a Project
Using PHP Sessions in a more sophisticated way and pass information easily on different modules of Project
Full Coding Exercises
Login Logout Admin System
Tips / Tricks
Code Re-usability
Strong Security Techniques on Public Pages
PDO layer for Database Operations
Enhancing UI UX in admin Panel so that the Admin can manage the different Components of CMS / Blog easily
Enhancing UI UX in Public Panel so that the Public / Users can easily interact with the different Functionalities of Blog easily.
Using PHP Sessions in a more sophisticated way and pass information easily on different modules of Project
Login Logout Admin System
URL Tracking
Font Awesome
PHP 5.6 and 7 Supports
Admin Info Update
Now if you want to learn PHP and start your professional career, Take this course now

This course is for complete beginners and for those who want to build projects in PHP.

Who this course is for:

Complete Beginners
PHP Aspirants
Designers who want to Start Back-End Programming
College Students
Basic knowledge
No Knowledge required of PHP. This course will teach students everything about PHP from Scratch
Basic HTML
PC of-course :)
What will you learn
All PHP Fundamentals and Building Blocks with practical implementation in Projects
Form Validation with most Secure way using Regular Expressions
Making web pages dynamic with the variety of PHP Techniques
Employee Management System CRUD Application in PHP From Scartch
Complete CMS ( Content Management System) with Admin-Panel
Getting Started with Bootstrap 4 FrameWork from scratch without using any Bootstrap Template
Using PHP Sessions in a more sophisticated way and pass information easily on different modules of Project
Stunning Blog with Commenting functionality
Powerful Web Forms which will be Free of Hack
Build Professional CRUD
CMS Based Projects
Full Coding Exercises
Login Logout Admin System
Tips / Tricks
Sending Email using PHP
Bootstrap 4.2.1
PDO
URL Tracking
Security Techniques on Public pages
Font Awsome
To continue:

PHP Interview Questions with Solutions: Prepare for PHP Interview

PHP Interview Questions with Solutions: Prepare for PHP Interview

Prepare for PHP Interviews. Set Variable With php.ini File. Logic & Output Behind PHP Code Segment. Class Concept, Error & Functions in PHP. Start PHP Now!

Description
In this course you will be introduced with some tricky questions that everyone face during their interview. In each solutions I have included some useful functions which we generally use at the time of development also. I have covered the following area in my course with Questions, Attractive Presentations and Practical Solutions that will help you to understand the logic behind PHP in a different way.

Some useful functions
PHP error types
Class concept
Access specifiers public, private and protected
Set variable with php.ini file
Operators introduced in PHP7
Logic and its output behind some code segment
Basic knowledge
PHP7, XAMPP Server, Notepad++
What will you learn
This course is build for the person who is facing interviews. Every question is well explained with the practical solution through videos. So that everybody can prepare themselves for the tricky questions asked during interviews
To continue: