PHP Registration Form is the most popular part of web applications. It is commonly used to create a new account on the website with a unique email address or username. Even It stores users’ registration data in the database so that they would be able to login to the website. It is also known as a signup form. Show
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. Contents Create a Registration Form Using PHP, MySQLBefore start coding, you should know the following basic points. These points will give you the best concept to create a registration form using PHP.
Read Also PHP Form Validation PHP Login Form 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 TableFirst of all, Create a MySQL database. You can also create it directly in PHPMyAdmin. Now, Create a Table in the MySQL database using the following query. Table Name – users 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, ) 2. Connect MySQL Database to PHPNow, 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); } ?> 3. Create a Registration Form Using HTMLConfigure the following steps to create a registration form
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--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <!--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 PHPBefore writing a script to create a registration form, you should understand the following points.
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; } } ?>
My SuggestionDear 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. How to connect registration form with database in php?For this you need to follow the following steps:. Step 1: Filter your HTML form requirements for your contact us web page. ... . Step 2: Create a database and a table in MySQL. ... . Step 3: Create HTML form for connecting to database. ... . Step 4: Create a PHP page to save data from HTML form to your MySQL database. ... . Step 5: All done!. How to put validation in php registration form?Assume we have the following form in a page named "test_form.php": <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
...
Your Input:. How to validate login form in php with database?Step 1- Create a HTML PHP Login Form. To create a login form, follow the steps mentioned below: ... . Step 2: Create a CSS Code for Website Design. ... . Step 3: Create a Database Table Using MySQL. ... . Step 4: Open a Connection to a MySQL Database. ... . Step 5 - Create a Logout Session. ... . Step 6 - Create a Code for the Home Page.. How to store registration details in database using php?This tutorial shows you an easy way to insert/save/store your form data into a MySQL database table using a simple PHP Code.. Create a Database Connection File.. Create Html form.. Create Insert. php file to Insert Data Into Database using PHP Script.. |