Scripts, Tips & Tricks
785

PHP Registration Form – Create Registration Form Using PHP

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.

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

registration-form/
    |__database.php
    |__registration-script.php
    |__registration-form.php
    |

1. Create a MySQL Database and Table

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

$query = “CREATE DATABASE codingstatus”;

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

CREATE TABLE `users` (
`id` int(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
`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,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

2. Connect MySQL Database to PHP

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

File Name –  database.php

<?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);
}
?>
[/bg_collapse]

 

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

<?php
require(‘registration-script.php’);
?>
<!DOCTYPE html>
<html lang=“en”>
<head>
<title>PHP Registration Form</title>
<meta charset=“utf-8”>
<meta name=“viewport” content=“width=device-width, initial-scale=1”>
<!–bootstrap4 library linked–>
<!–custom style–>
<style type=“text/css”>
.registration-form{
background: #f7f7f7;
padding: 20px;
border: 1px solid orange;
margin: 50px 0px;
}
.err-msg{
color:red;
}
.registration-form form{
border: 1px solid #e8e8e8;
padding: 10px;
background: #f3f3f3;
}
</style>
</head>
<body>
<div class=“container-fluid”>
<div class=“row”>
<div class=“col-sm-4”>
</div>
<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; }?>
</p>
</div>
<!--//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; } ?>
</p>
</div>
<!--// 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; } ?>
</p>
</div>
<!--//Password//–>
<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; } ?>
</p>
</div>
<!--//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; } ?>
</p>
</div>
<button type=“submit” class=“btn btn-danger” name=“submit”>Register Now</button>
</form>
</div>
</div>
<div class=“col-sm-4”>
</div>
</div>
</div>
</body>
</html>

 

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

<?php
require_once(‘database.php’);
$db= $conn; // update with your database connection
// by default, error messages are empty
$register=$valid=$fnameErr=$lnameErr=$emailErr=$passErr=$cpassErr=;
// by default,set input values are empty
$set_firstName=$set_lastName=$set_email=;
extract($_POST);
if(isset($_POST[‘submit’]))
{
//input fields are Validated with regular expression
$validName=“/^[a-zA-Z ]*$/”;
$validEmail=”/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/”;
$uppercasePassword = “/(?=.*?[A-Z])/”;
$lowercasePassword = “/(?=.*?[a-z])/”;
$digitPassword = “/(?=.*?[0-9])/”;
$spacesPassword = “/^$|\s+/”;
$symbolPassword = “/(?=.*?[#?!@$%^&*-])/”;
$minEightPassword = “/.{8,}/”;
// First Name Validation
if(empty($first_name)){
$fnameErr=“First Name is Required”;
}
else if (!preg_match($validName,$first_name)) {
$fnameErr=“Digits are not allowed”;
}else{
$fnameErr=true;
}
// Last Name Validation
if(empty($last_name)){
$lnameErr=“Last Name is required”;
}
else if (!preg_match($validName,$last_name)) {
$lnameErr=“Digit are not allowed”;
}
else{
$lnameErr=true;
}
//Email Address Validation
if(empty($email)){
$emailErr=“Email is Required”;
}
else if (!preg_match($validEmail,$email)) {
$emailErr=“Invalid Email Address”;
}
else{
$emailErr=true;
}
// password validation
if(empty($password)){
$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”;
}
else{
$passErr=true;
}
// form validation for confirm password
if($cpassword!=$password){
$cpassErr=“Confirm Password doest Matched”;
}
else{
$cpassErr=true;
}
// 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
$checkEmail=unique_email($email);
if($checkEmail)
{
$register=$email.” is already exist“;
}else{
// Insert data
$register=register($firstName,$lastName,$email,$password);
}
}else{
// set input values is empty until input field is invalid
$set_firstName=$first_name;
$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;
}else{
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(?,?,?,?)“;
$query=$db->prepare($sql);
$query->bind_param(‘ssss’,$firstName,$lastName,$email,$password);
$exec= $query->execute();
if($exec==true)
{
return “You are registered successfully“;
}
else
{
return “Error: ” . $sql . “<br>” .$db->error;
}
}
?>

 

Summary

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…

Share using
Tags:

More Similar Posts

Most Viewed Posts
Menu