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

<html>
<head>
<title>Registration </title>
</head>
<body>
<form name="reg_form" id="reg_form" method="post" action="confirm.php">
<table>
<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>
</table>
</form>
</body>
</html>

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

  <?php
$username=isset($_POST["username"])?$_POST["username"]:"";
$password=isset($_POST["password"])?$_POST["password"]:"";
$confirm=isset($_POST["confirm"])?$_POST["confirm"]:"";
if(!empty($username)&&!empty($password)){
if($password!=$confirm)
header("location:registration.php?msg=Password does not be match.");
$host="localhost";
$user="mysql_user_name";
$pass="mysql_password";
///open connection
$link=mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db("databse_name",$link);
$query="SELECT * FROM users WHERE username='".mysql_escape_string($username)."'";
$result=mysql_query($query);
//count no of rows
$count=mysql_num_rows($result);
if($count==1){
header("location:registration.php?msg=username already exists");
}
else
{
$qry="INSERT INTO users(username,password)VALUES('".mysql_escape_string($username)."' ,'".mysql_escape_string($password)."')";
mysql_query($qry);
echo "You are successfully registered.";
}
mysql_close($link);

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


 

Happy coding