Php registration form | php registration script with mysql

Here we are going to make simple php registration form and registration script.Php registration script, we are using Mysql database to store users' data .

First we need to create user table in database with three fields id,username and password. the query is given below:

CREATE TABLE users(id int AUTO_INCREMENT PRIMARY KEY,username varchar(45), password varchar(15))

The above query will create table. .

Our first step is completed for php registration script. Now second step is making registration form

<title>Registration </title>
<form name="reg_form" id="reg_form" method="post" action="confirm.php">
<tr><td colspan="2"><?php echo isset($_GET["msg"])?$_GET["msg"]:"";?> </td></tr>
<tr><td>Username</td><td><input type="text" name="username" id="username" /> </td></tr>
<tr><td>Password</td><td><input type="password" name="password" id="password" /> </td></tr>
<tr><td>confirm</td><td><input type="password" name="confirm" id="confirm" /> </td></tr>

<tr><td></td><td><input type="submit" name="btnsubmit" id="btnsubmit" /></td></tr>

Our php registration form is ready.As you see, here action attribute is set confirm.php. This is the page, where our form's data is submitted.

And on this page script will validate username ,password and confirm.Php script also checks from table users whether username exists or not. User is returned to registration page with message if

1. username or password is blank.
2 password and confirm do not match.
3.username already exists.

And line <?php echo isset($_GET["msg"])?$_GET["msg"]:"";?>. prints error message.

Now coding for confirm.php

header("location:registration.php?msg=Password does not be match.");
///open connection
$link=mysql_connect($host,$user,$pass) or die(mysql_error());
$query="SELECT * FROM users WHERE username='".mysql_escape_string($username)."'";
//count no of rows
header("location:registration.php?msg=username already exists");
$qry="INSERT INTO users(username,password)VALUES('".mysql_escape_string($username)."' ,'".mysql_escape_string($password)."')";
echo "You are successfully registered.";

header("location:registration.php?msg=Username or password cannot be blank.");


Happy coding