Nice user Registration system in php MySQL with email and username already exist - Web Development and Web Design Codes

Latest

Wednesday, February 14, 2018

Nice user Registration system in php MySQL with email and username already exist

PHP MySQL User Registration System with Database Validation

PHP MySQL User Registration System with Database Validation
PHP MySQL User Registration System with Database Validation
Hi Friends today we are going to learn how to create registration system with database validation in php MySQL.. this script will help you to prevent duplicate username and email submit..
Follow the below Example..
=>Database and Table:
   Database Name: test
   Table Name: user
 Table Structure for Table user

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(20) NOT NULL,
  `sex` varchar(10) NOT NULL,
  `country` varchar(20) NOT NULL,
  `fullname` varchar(20) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Just Follow the step by Step Process to Create user Registration system in php MySQL:
Use The below codes to create html registration form
<div align="center">
<h3>PHP MySQL Registration System With Email and UserName Already Exist</h3>
<form method="POST">
<table>
    <tr>
    <td>UserName:</td>
    <td><input type="text" name="name"/>
       <div><?php if(isset($nameerror)){
    echo '<div class=error>'.$nameerror.'</div>';}?></div>
    
    </td>
 </tr>
 <tr>
    <td>Email:</td>
    <td><input type="text" name="email"/>
       <div><?php if(isset($emailerror)){
    echo '<div class=error>'.$emailerror.'</div>';}?></div>
    
    </td>
 </tr>
 <tr>
    <td>Password:</td>
    <td><input type="text" name="password"/>
       <div><?php if(isset($passerror)){
    echo '<div class=error>'.$passerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Full Name:</td>
    <td><input type="text" name="fname"/>
       <div><?php if(isset($fnameerror)){
    echo '<div class=error>'.$fnameerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Sex:</td>
    <td><input type="radio" name="sex" value="female"/>Female
    <input type="radio" name="sex" value="male"/> Male
    <input type="radio" name="sex" value="other"/>Other
       <div><?php if(isset($sexerror)){
    echo '<div class=error>'.$sexerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Country:</td>
    <td>
    <select type="text" name="country">
    <option value="">Select Country</option>
       <option value="india">India</option>
    <option value="usa">United States</option>
    <option value="japan">Japan</option>
    <option value="russia">Russia</option>
    </select>
       <div><?php if(isset($countryerror)){
    echo '<div class=error>'.$countryerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td></td>
    <td><input type="submit" name="submit"/></td>
 </tr>
</table>
</form>
<?php if(isset($success)){
    echo $success;}?>
<?php if(isset($error)){
    echo $error;}?>
    </div>
Use The Below CSS Codes to Design Your HTML Page

<style>
  .error{
   color:red;
   font-weight:bold;
   font-style:italic;
  }
  input[type=text]{
   padding:5px;
   border-radius:4px;
  }
  input[type=submit]{
   padding:7px;
   background:black;
   color:white;
   border-radius:4px;
  }
  input[type='radio']:after {
        width: 15px;
        height: 15px;
        border-radius: 15px;
        top: -2px;
        left: -1px;
        position: relative;
        background-color: #d1d3d1;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }
    input[type='radio']:checked:after {
        width: 15px;
        height: 15px;
        border-radius: 15px;
        top: -2px;
        left: -1px;
        position: relative;
        background-color: #ffa500;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }
 select {
    padding:3px;
    margin: 0;
 font-size:15px;
 width:185px;
    border-radius:4px;
    background: white;
    color:#888;

    -webkit-appearance:none;
    -moz-appearance:none;
    appearance:none;
    cursor:pointer;
}
</style>

Now Use The below php codes to validate username ,email and other fields ..

<?php
$conn=new mysqli('localhost','root','','test');
if(isset($_POST['submit'])){
 //UserName Checking
 if(!empty($_POST['name'])){
  //Cheking for White Space
  $username=preg_match('/\s/',$_POST['name']);
  if($username==0){
  $result=mysqli_query($conn,"SELECT*FROM user WHERE username='".$_POST['name']."'");
  $count=$result->num_rows;
  if($count==1){
   $error='';
   $nameerror="Sorry UserName <font color=green> $_POST[name]</font> Already Taken";
  }
 }else{
  $error='';
  $nameerror='White Space are Not Allowed';
 }
 }else{
  $nameerror='UserName Can not Be Empty';
  $error='';
 }
 //Email Checking and Validation
 if(!empty($_POST['email'])){
  if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
   //Checking for Email Avaliablity
   $result=mysqli_query($conn,"SELECT*FROM user WHERE email='".$_POST['email']."'");
   $count=$result->num_rows;
   if($count==1){
    $error='';
    $emailerror="Sorry UserName <font color=green> $_POST[email]</font> Already Taken";
   }
  }else{
   $error='';
   $emailerror='Invalid Email Address';
  }
 }else{
  $error='';
  $emailerror='Email Address can not be Empty';
 }
 if(!empty($_POST['password'])){
    if(strlen($_POST['password'])<8){
     $passerror='Your Password Should not be less Than 8 Characters';
     $error='';
    }
   }else{
     $passerror='Your Password is Empty';
  $error='';
   }
   //Full Name Validation
   if(empty($_POST['fname'])){
    $fnameerror="Your Full Name Can Not be Empty";
    $error='';
   }
   //Sex Checking
   if(empty($_POST['sex'])){
    $sexerror="Your Sex Can Not be Empty";
    $error='';
   }
   //Country Checking
   if(empty($_POST['country'])){
    $countryerror="Your Country Can Not be Empty";
   $error='';
   }
 
 //Inserting Data
 if(!isset($error)){
  //Inserting User Info Into Database
    $username=mysqli_real_escape_string($conn,$_POST['name']);
    $password=mysqli_real_escape_string($conn,$_POST['password']);
    $email=mysqli_real_escape_string($conn,$_POST['email']);
    $fname=mysqli_real_escape_string($conn,$_POST['fname']);
    $sex=mysqli_real_escape_string($conn,$_POST['sex']);
    $country=mysqli_real_escape_string($conn,$_POST['country']);
    $result=mysqli_query($conn,"INSERT INTO user(username,password,email,sex,country,fullname)
    VALUES('$username','$password','$email','$sex','$country','$fname')");
    if($result){
     $success="Thank You.Your Registration Successfull";
    }else{
     $error="Sorry Something went Wrong.Please Try again";
    }
 }
}

?>

Complete Codes: PHP User Registration System Complete Codes:
<?php
$conn=new mysqli('localhost','root','','test');
if(isset($_POST['submit'])){
 //UserName Checking
 if(!empty($_POST['name'])){
  //Cheking for White Space
  $username=preg_match('/\s/',$_POST['name']);
  if($username==0){
  $result=mysqli_query($conn,"SELECT*FROM user WHERE username='".$_POST['name']."'");
  $count=$result->num_rows;
  if($count==1){
   $error='';
   $nameerror="Sorry UserName <font color=green> $_POST[name]</font> Already Taken";
  }
 }else{
  $error='';
  $nameerror='White Space are Not Allowed';
 }
 }else{
  $nameerror='UserName Can not Be Empty';
  $error='';
 }
 //Email Checking and Validation
 if(!empty($_POST['email'])){
  if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
   //Checking for Email Avaliablity
   $result=mysqli_query($conn,"SELECT*FROM user WHERE email='".$_POST['email']."'");
   $count=$result->num_rows;
   if($count==1){
    $error='';
    $emailerror="Sorry UserName <font color=green> $_POST[email]</font> Already Taken";
   }
  }else{
   $error='';
   $emailerror='Invalid Email Address';
  }
 }else{
  $error='';
  $emailerror='Email Address can not be Empty';
 }
 if(!empty($_POST['password'])){
    if(strlen($_POST['password'])<8){
     $passerror='Your Password Should not be less Than 8 Characters';
     $error='';
    }
   }else{
     $passerror='Your Password is Empty';
  $error='';
   }
   //Full Name Validation
   if(empty($_POST['fname'])){
    $fnameerror="Your Full Name Can Not be Empty";
    $error='';
   }
   //Sex Checking
   if(empty($_POST['sex'])){
    $sexerror="Your Sex Can Not be Empty";
    $error='';
   }
   //Country Checking
   if(empty($_POST['country'])){
    $countryerror="Your Country Can Not be Empty";
   $error='';
   }
 
 //Inserting Data
 if(!isset($error)){
  //Inserting User Info Into Database
    $username=mysqli_real_escape_string($conn,$_POST['name']);
    $password=mysqli_real_escape_string($conn,$_POST['password']);
    $email=mysqli_real_escape_string($conn,$_POST['email']);
    $fname=mysqli_real_escape_string($conn,$_POST['fname']);
    $sex=mysqli_real_escape_string($conn,$_POST['sex']);
    $country=mysqli_real_escape_string($conn,$_POST['country']);
    $result=mysqli_query($conn,"INSERT INTO user(username,password,email,sex,country,fullname)
    VALUES('$username','$password','$email','$sex','$country','$fname')");
    if($result){
     $success="Thank You.Your Registration Successfull";
    }else{
     $error="Sorry Something went Wrong.Please Try again";
    }
 }
}

?>


<style>
  .error{
   color:red;
   font-weight:bold;
   font-style:italic;
  }
  input[type=text]{
   padding:5px;
   border-radius:4px;
  }
  input[type=submit]{
   padding:7px;
   background:black;
   color:white;
   border-radius:4px;
   cursor:pointer;
  }
  input[type='radio']:after {
        width: 15px;
        height: 15px;
        border-radius: 15px;
        top: -2px;
        left: -1px;
        position: relative;
        background-color: #d1d3d1;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }
    input[type='radio']:checked:after {
        width: 15px;
        height: 15px;
        border-radius: 15px;
        top: -2px;
        left: -1px;
        position: relative;
        background-color: #ffa500;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }
 select {
    padding:3px;
    margin: 0;
 font-size:15px;
 width:185px;
    border-radius:4px;
    background: white;
    color:#888;

    -webkit-appearance:none;
    -moz-appearance:none;
    appearance:none;
    cursor:pointer;
}
</style>


<div align="center">
<h3>PHP MySQL Registration System With Email and UserName Already Exist</h3>
<form method="POST">
<table width="50%">
    <tr>
    <td>UserName:</td>
    <td><input type="text" name="name"/>
       <div><?php if(isset($nameerror)){
    echo '<div class=error>'.$nameerror.'</div>';}?></div>
    
    </td>
 </tr>
 <tr>
    <td>Email:</td>
    <td><input type="text" name="email"/>
       <div><?php if(isset($emailerror)){
    echo '<div class=error>'.$emailerror.'</div>';}?></div>
    
    </td>
 </tr>
 <tr>
    <td>Password:</td>
    <td><input type="text" name="password"/>
       <div><?php if(isset($passerror)){
    echo '<div class=error>'.$passerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Full Name:</td>
    <td><input type="text" name="fname"/>
       <div><?php if(isset($fnameerror)){
    echo '<div class=error>'.$fnameerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Sex:</td>
    <td><input type="radio" name="sex" value="female"/> Female
    <input type="radio" name="sex" value="male"/> Male
    <input type="radio" name="sex" value="other"/>Other
       <div><?php if(isset($sexerror)){
    echo '<div class=error>'.$sexerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td>Country:</td>
    <td>
    <select type="text" name="country">
    <option value="">Select Country</option>
       <option value="india">India</option>
    <option value="usa">United States</option>
    <option value="japan">Japan</option>
    <option value="russia">Russia</option>
    </select>
       <div><?php if(isset($countryerror)){
    echo '<div class=error>'.$countryerror.'</div>';}?></div>
    
    </td>
 </tr>
 
 <tr>
    <td></td>
    <td><input type="submit" name="submit"/></td>
 </tr>
</table>
</form>
<?php if(isset($success)){
    echo $success;}?>
<?php if(isset($error)){
    echo $error;}?>
    </div>

That's it Friends how to create Nice user Registration system in php MySQL with email and username already exist..Please Share and Keep Visiting This Blog..Thank You...

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.