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.

Steps to creating Simple User Registration Script in PHP and MySql

  1. Create a database
  2. Creating table with five fields
    • id
    • username
    • email
    • password
    • active
  3. Create form in html
  4. Adding styles to form
  5. Connect to the database using php
  6. Writing logic for user registration script in php

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 sql(below) into your database

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.

<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


<!-- 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


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.

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

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

 <?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 } ?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here