DEVELOPMENT


This tutorial will show you how to create a simple user registration form with php and mysql. Here, form validation is achieved by using PHP, however i not recommend you to do like this. Jquery function is a better solution. The code is straight forward, so no explaination is provided.

THE DATABASE



INDEX.PHP
<html>
<title>Sign up </title> 
<head><link rel="stylesheet" href="style.css" type="text/css" /></head>
<body>

<?php

function filter($data) {
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);
 $data=mysql_real_escape_string($data);
 return $data;
 } 


if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
$c=0;
$name=filter($_POST["name"]);
$email=filter($_POST["email"]);
$mobile=filter($_POST["mobile"]);
$password=filter($_POST["password"]);
$ip=$_SERVER['REMOTE_ADDR'] ;
$id=rand(10000,999999);
 
$var=mktime(0,0,0,date("m"),date("d"),date("y"));
$date=date("m/d/y",$var); 
 echo "<div class=\"err\">";
if (empty($_POST["name"])) 
 { $c=1;
 echo "Name is required.<br>";
} else
{ 
 if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
 { $c=1; 
 echo "Only letters and white space allowed.<br>";
 
 }
} 

if (empty($_POST["email"]))
{ $c=1; 
 echo "E-mail is required.<br>"; 
} 
else
{
 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
 { 
 $c=1; 
 echo "Invalid e-mail format.<br>";
 }
 
}
if (empty($_POST["mobile"])) 
{ $c=1;
 echo "Mobile is required.<br>";
 
} else 
{ 
 if (!preg_match("/[0-9]/",$mobile) || strlen($_POST["mobile"])<=9) 
 {
 $c=1;
 echo "Enter valid mobile-no.<br>" ;
 }
 
} 
if (empty($_POST["password"])) 
{ $c=1;
 echo "Password is required.<br>";
} 
else 
{ if(strlen($_POST["password"])<6)
{
 $c=1; 
 echo "Enter password greater then 6 word.<br>";
}else
{
 if (empty($_POST["confirm"])) 
 { $c=1;

 echo "Confirm your password.<br>";
 } 
 else
 {
 
 if($_POST["password"]!=$_POST["confirm"])
 { $c=1;
 
 echo "Password not match.<br>";

 } 
 } 
}
} 
 
echo "</div>";
$d=0; 
$mysqli = new mysqli("localhost", "root", "", "site1_db");
if(($mysqli->connect_error)){ 
 die("Server Error");
}
if($c==0)
{ 
 $sql = "SELECT * FROM user"; 
 $result = $mysqli->query($sql);
 if ($result->num_rows > 0) {
 while($row = $result->fetch_assoc()) {
 if($row['email']==$email)
 { echo "<div class=\"err1\">Sorry, You are already registered.</div>";
 $d=1;
 }
 }
 }
if($d==0)
{
 if ($sper = $mysqli->prepare("insert into user(name,email,mobileno,password,Primaryid,date,ipadd) values (?,?,?,?,?,?,?)")) {
 $sper->bind_param("sssssss", $name,$email,$mobile,$password,$id,$date,$ip);
 if($sper->execute())
 {
 echo "<div class=\"scs\">Cong, You are successfully registered.</div>"; 
 } 
 else
 {
 echo "<div class=\"err1\">Server Error.</div>";
 }
 $sper->close();
 } 
 }
}
else
{
 echo "<div class=\"err1\">Please, Correct the following error.</div>";
}
}
?>
<div class="err" style="color:#333">Sign up. All fields are necessary. (E-mail - unique)</div>
<div class="wrap">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="Post">
<table class="frm" >
<tr ><td>Name : </td> <td> <input name ="name" type="text" value="" maxlength="25"/>*</td></tr>
<tr><td >Email : </td> <td><input name ="email" type="text" maxlength="100"/>*</td></tr>
<tr ><td >Mobile no. : </td> <td><input name ="mobile" type="numeric" maxlength="12"/>*</td></tr>
<tr ><td >Password : </td> <td><input name ="password" type="password" maxlength="25"/>*</td></tr>
<tr ><td >Re-enter Password : <td><input name ="confirm" type="password" maxlength="25"/>*</td></tr>
<tr><td></td><td><input type="submit" value="Create Account" /></td></tr>
</table>
</form>
</div> 
</body>
</html>
    
STYLE.CSS
body{
top:0px;
margin:0;
padding:0;
font-family: Arial;
}
.frm
{
margin-top: 20px;
color:#333333;
font-size: 14px;
background: #eee;
padding: 5px;
width:320px;
}
.err
{
color:red;  
font-size: 14px;
margin: 25 auto;
width:50%;
}
.err1
{
position:fixed;
top:0;
height: 20px;
color:white;
font-size: 18px;    
background: red;
width:100%;
}
.scs
{
position:fixed;
top:0;
height: 20px;
color:white;
font-size: 18px;    
background: green;
width:100%;
}
.wrap
{
width: 50%; 
margin: 0 auto;
}



Leave a Comment  
Download Code(35)





All rights reserved, the content is copyrighted to the author.

RELATED POST