6 Steps to Create Simple User Registration Script in PHP and MySql
This is a Simple User Registration Script in PHP and MySQL. By using this user can easily register, filling username, email, password fields. After submitting form, the data will be stored in a database table. So that next time user can login with the login credentials which are used while user registration.
Here these are a series of articles
These scripts are for beginner learners, who want to learn the concept of user registration, login, login activation and forgot password.
- Simple User Registration Script in PHP and MySQL
- Simple Login Script in PHP and MySQL
- User Login with activation check using PHP and MySQL
- Send Forgotten Password by mail using PHP and MySQL
- Check Username Availability With jQuery In PHP And MySql
Steps to creating Simple User Registration Script in PHP and MySql
- Create a database
- Creating table with five fields
- id
- username
- password
- active
- Create form in html
- Adding styles to form
- Connect to the database using php
- Writing logic for user registration script in php
Below is the Video on PHP User Login Registration Script With All Features

Do you want to Build Secure Members Area for your Website?
I've good News for you! Created a Video Course with 5 Hours of Content to create secure members area section
1. Create a Database
For creating a database, login to phpmyadmin, then click on Databases tab. And in the input field enter database name and click on create database button.
2. Creating a Database table
After creating Database, Create a database table for users, it’s simple table with user name, email, password, active and an id as primary key and also it is auto incremented.
- id is the primary key and auto incremented.
- Username field will be used for storing username
- email field will be used for storing email address of user, this will be used to send forgotten password by email in this series.
- Password field will store password of user in normal format.
- active state is for checking the user is active or blocked user, If the user is blocked then he can’t login to system.
Import this below sql into your database.
1 2 3 4 5 6 7 8 9 | CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `active` tinyint(1) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) ) |
3. Create form in html
Save this file as register.php, Here this is simple html for generating form. This form contains user name field, email, password a submit button and a link to login.php file. Here i’m adding some classes to the HTML, Later i can add some styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <div class="container"> <form class="form-signin" method="POST"> <h2 class="form-signin-heading">Please Register</h2> <div class="input-group"> <span class="input-group-addon" id="basic-addon1">@</span> <input type="text" name="username" class="form-control" placeholder="Username" required> </div> <label for="inputEmail" class="sr-only">Email address</label> <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button> <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a> </form> </div> |
4. Adding styles to form
Here i’ve added some styles to form, so that it will look good. I’m using Bootstrap with some custom styles.
Put this code in head section of register.php file
1 2 3 4 5 6 7 8 9 10 | <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" > <link rel="stylesheet" href="styles.css" > <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> |
Save this code as styles.css file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | body { padding-top: 40px; padding-bottom: 40px; background-color: #eee; } .form-signin { max-width: 330px; padding: 15px; margin: 0 auto; } .form-signin .form-signin-heading, .form-signin .checkbox { margin-bottom: 10px; } .form-signin .checkbox { font-weight: normal; } .form-signin .form-control { position: relative; height: auto; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; padding: 10px; font-size: 16px; } .form-signin .form-control:focus { z-index: 2; } .form-signin input[type="email"] { margin-bottom: -1px; border-bottom-right-radius: 0; border-bottom-left-radius: 0; } .form-signin input[type="password"] { margin-bottom: 10px; border-top-left-radius: 0; border-top-right-radius: 0; } |
5. Connect to the database
Create a file with name connect.php. Here i’m connecting to database and selecting the database.
User correct database name & user credentials.
1 2 3 4 5 6 7 8 9 | <?php $connection = mysqli_connect('localhost', 'root', 'Rvm@i[9)0?~='); if (!$connection){ die("Database Connection Failed" . mysqli_error($connection)); } $select_db = mysqli_select_db($connection, 'test'); if (!$select_db){ die("Database Selection Failed" . mysqli_error($connection)); } |
6. Writing logic for user registration script in php
All the registration process done in one single php file, in this file we will display an html form for user to register. In the PHP section we will first connect to the database, then we will select the database, if the values are posted, then we are running a MySQL query it will insert the values into the database.
Add this code register.php file above the html code. That means right before step 3 code. If you have any dobut, you can check the final code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php require('connect.php'); // If the values are posted, insert them into the database. if (isset($_POST['username']) && isset($_POST['password'])){ $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $query = "INSERT INTO `user` (username, password, email) VALUES ('$username', '$password', '$email')"; $result = mysqli_query($connection, $query); if($result){ $smsg = "User Created Successfully."; }else{ $fmsg ="User Registration Failed"; } } ?> |
Add this code after opening form tag, this code is used to display the status message.
1 2 | <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?> <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?> |
Complete Code of Register.php
Next Steps After User Registration
About code is just simple user registration code for educational purpose, if you are implementing this code in the real-world application then you should use these techniques.
Sanitize the Data
First of all, you should sanitize the data. This will helps to stop the sql injection & cross-site scripting XSS attacks. To stop sql injection use mysqli_real_escape_sting PHP function. And for XSS attacks you should use htmlentities PHP function.
Example of the code is below, you should apply this to all the submitted values.
1 | $username = mysqli_real_escape_string($connection, $_POST['username']); |
Password Encryption
In the above code, you can see I’m storing passwords in plain text method. That is not recommended method to store passwords in the database. Use password_hash PHP function to store the passwords in database, it’s a secure method to store the password in database.
If you follow these above methods, you can create a secure user registration process for your web application.
If you want to learn other things you can read other posts – registration, login, user login with activation check, and sending forgotten password by email, Check Username Availability .
I hope this article helped you to learn user login using PHP and MySQL . To get latest news and updates follow us on twitter & facebook, subscribe to newsletter. If you have any feedback please let us know by using comment form.

Do you want to Build Secure Members Area for your Website?
I've good News for you! Created a Video Course with 5 Hours of Content to create secure members area section
registration form in php simple registration form in php PHP registration script php registration form php code for registration form user registration form in php php register script php code for registration php registration form with database connection code php signup script