Check Availability of Name in Registration Page in PHP

First we will create a registration page that contains three fields such as name, address and password. While entering a name in the textbox, if it already exists in the database it will show a message ("Sorry! you can't take this name") and vice-versa.
 
 Table Creation
 
 -- phpMyAdmin SQL Dump
 -- version 2.10.1
 -- http://www.phpmyadmin.net
 --
 -- Host: localhost
 -- Generation Time: Feb 17, 2013 at 11:01 AM
 -- Server version: 5.0.45
 -- PHP Version: 5.2.5
 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
 --
 -- Database: `home_work`
 --
 -- --------------------------------------------------------
 --
 -- Table structure for table `employee`
 --
 CREATE TABLE `employee` (
 `id` int(12) NOT NULL auto_increment,
 `name` varchar(100) default NULL,
 `address` varchar(100) NOT NULL,
 `password` varchar(50) default NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `name` (`name`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
 --
 -- Dumping data for table `employee`
 --
 INSERT INTO `employee` (`id`, `name`, `address`, `password`) VALUES
 (1, 'Raj', 'Pune', 'cac5ff630494aa784ce97b9fafac2500');
 config.php
 <?php
 $DBHOST = "localhost";
 $DBNAME = "home_work";
 $DBUSER = "root";
 $sLink = mysql_connect($DBHOST,$DBUSER,'') or die('Connection with MySql Server failed');
 mysql_select_db($DBNAME, $sLink) or die('MySql DB was not found');
 ?>
 registration.php
 <?php
 session_start();
 include('config.php');
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Employee Registration</title>
 
 <link href="style.css" rel="stylesheet" type="text/css" />
 <style type="text/css">
 .style1 {
 text-align: left;
 }
 .style2 {
 text-align: left;
 color: #0000FF;
 }
 .style3 {
 color: #0000FF;
 }
 .style4 {
 color: #FF0000;
 }
 </style>
 <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
 //called when key is pressed in textbox
 $("#name").keypress(function (e)
 {
 $("#error").hide();
 $("#autoSuggestionsList").hide();
 //if the letter is not digit then display error
 if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)&& (e.which<65 || e.which>90)&& (e.which<97 || e.which>122))
 {
 //display error message
 $("#error").html("No Special Characters").show();
 return false;
 }
 });});
 function lookup(name)
 {
 if(name.length <3)
 {
 $('#suggestions').show();
 $('#autoSuggestionsList').hide();
 }
 else
 {
 $('#suggestions').hide();
 $.post("Checkname.php",{user: ""+name+""},function(data){
 $('#autoSuggestionsList').html(data).show();
 });
 }
 }
 
 </script>
 <script language="javascript" type="text/javascript">
 
 function validation()
 {
 var formName=document.frm;
 if(formName.name.value == "")
 {
 document.getElementById("name_label").innerHTML='Please Enter Name';
 formName.name.focus();
 return false;
 }
 else
 {
 document.getElementById("name_label").innerHTML='';
 }
 
 if(formName.address.value == "")
 {
 document.getElementById("address_label").innerHTML='Please Enter Address';
 formName.address.focus();
 return false;
 }
 else
 {
 document.getElementById("address_label").innerHTML='';
 }
 
 if(formName.password.value == "")
 {
 document.getElementById("password_label").innerHTML='Please Enter Password';
 formName.password.focus();
 return false;
 }
 else
 {
 document.getElementById("password_label").innerHTML='';
 }
 }
 
 </script>
 </head>
 <?php
 if($_REQUEST["action"]=='register')
 {
 $_SESSION["name"]=$_POST["name"];
 $name=mysql_real_escape_string($_SESSION["name"]);
 $address=mysql_real_escape_string($_POST['address']);
 $password=mysql_real_escape_string($_POST['password']);
 $password=md5($password); // Encrypted Password
 $sql="Insert into employee(name,address,password) values('$name','$address','$password');";
 $result=mysql_query($sql);
 echo "Registration Successfully Completed";
 echo "<script>window.location.href='welcome.php'</script>";
 }
 ?>
 <body>
 <form method="post" id="frm" name="frm" action="registration.php?action=register" onSubmit="return validation();">
 <table width="500" border="0">
 <tr>
 <td class="deepbluetextbold"><b>Employee Registration</b></td>
 </tr>
 <tr>
 <td class="colouredCell">Name*</td>
 <td>
 <input name="name" type="text" id="name" onkeyup="lookup(this.value);"autocomplete="off" value="<?php echo $_REQUEST[name]; ?>"/>&nbsp; <label id="name_label" class="level_msg"></label></td>
 </tr>
 <tr><td><font color="red"><div id="suggestions" style="display: none;">More than 2 characters</div>
 <div style="display: none;" id="autoSuggestionsList"></div>
 <div style="display: none;" id="error"></div></td></tr>
 <tr>
 <td class="colouredCell">Address*</td>
 <td><input type="text" name="address" id="address" autocomplete="off" value="<?php echo $_REQUEST[address]; ?>"/>&nbsp; <label id="address_label" class="level_msg"></label></td>
 </tr>
 <tr>
 <td class="colouredCell">Password*</td>
 <td><input type="password" name="password" id="password" autocomplete="off"/>&nbsp; <label id="password_label" class="level_msg"></td>
 </tr>
 </table>
 </tr>
 <tr>
 <td>
 <input type="submit" name="Submit" value="Register" /> </td>
 <td>&nbsp;</td>
 </tr>
 </table>
 </form>
 </body>
 </html>
 Checkname.php
 <?php
 session_start();
 $str=$_POST['user'];
 $host="localhost";
 $username="root";
 $password="";
 $db_name="home_work";
 $con = mysql_connect($host,$username,$password) or die(mysql_error());;
 mysql_select_db($db_name, $con) or die(mysql_error());
 $sql= "SELECT name FROM employee WHERE name='".$str."'";
 $result=mysql_query($sql,$con) or die(mysql_error());
 $count=0;
 while($row = mysql_fetch_array($result, MYSQL_ASSOC))
 {
 $count++;
 }
 if($count==0)
 {
 echo "You can take this name";
 }
 else
 {
 echo "Sorry! you can't take this name";
 }
 ?>
 welcome.php
 <?php
 session_start();
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Welcome</title>
 </head>
 <body>
 <form action="welcome.php" method="get" name="welcome">
 <b>Registration Successfully Completed</b><br/>
 <?php echo '<b>Welcome:::</b>'.$_SESSION["name"]; ?>
 </form>
 </body>
 </html>
 
 Running the application
 
 Run the WampServer then write the following line in the URL:
 
 http://localhost/Availability Username/

php1.gif

When we insert Raj the second time for registration it displays the message "Sorry! You can't take this name".

php2.gif