php check if username name already exist or not in mysql table

Share:
PHP CHECK IF USERNAME  ALREADY EXIST OR NOT IN MYSQL TABLE

Hello Readers, In this article we will see how to check a username already present or not in MySql table using PHP. In the previous tutorial we have seen how to create a simple registration form with PHP and MySql table, So in this article we are going to enhance that previous tutorial by adding this feature ot check username availability with PHP and MySql. Below code is to  check username already present or not in table.

-First extracting the form parameters and storing in respective variables.

-Creating connection with MySql DataBase.

-Writing the query to check if username already present or not in MySql table.

-Finally executing the query and checking if username already exist or not in MySql table.
<?php

session_start();

$msg="";

if($_SERVER["REQUEST_METHOD"] == "POST")
{

$id=$_POST['id'];

$pwd=$_POST['pwd']; 

$connect=mysqli_connect("localhost","root","","dbName");

$user=mysqli_query($connect,"SELECT username FROM signup where username='$id' ");

    $result=mysqli_num_rows($user);

    if($result>0)
{
$msg="This username already in use please choose different one...";
 }
    else
{
     $query=mysqli_query($connect,"INSERT INTO signup VALUES('$id','$pwd')");
     
     $_SESSION['username'] = $id;
       
 echo "<script>
               alert('Success...'); 
              window.location='ok.php';
        </script>"; 
    }      
}

?>

<!DOCTYPE html>
<html lang="en">   
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>User_Registration</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
     
      <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  </head>
  <body >
     
      <!--JavaScript Starts-->
     
<script>
    function Validate(){
             var id=document.getElementById('id');
             var pwd=document.getElementById('pwd');
             var cpwd=document.getElementById('cpwd');
             var sc=document.getElementById('sc');
   
             var iderror=document.getElementById('iderror');
             var pwderror=document.getElementById('pwderror');
             var cpwderror=document.getElementById('cpwderror');
            
             //reset error msg
             iderror.innerHTML="";
             pwderror.innerHTML="";
             cpwderror.innerHTML="";
       
          if(id.value.length<6  || id.value.length > 20){
             iderror.innerHTML="Username Must be 6 To 20 Character Long";
             iderror.style.color="red"
           }   
         else if(pwd.value.length<6 || pwd.value.length >15){
             pwderror.innerHTML="Password Must Be 10 To 15 Character Long";
             pwderror.style.color="red";  
          }
         else if(cpwd.value!=pwd.value){
           cpwderror.innerHTML="Password Mismatch";
           cpwderror.style.color="red"
       }
       else{
           document.myForm.submit();
       }
    }
    </script>
      <!--JavaScript Ends-->
     
      <div class="container" style="margin-top:-32px">
          <div class="col-lg-4"></div>
             <div class="col-lg-4">        
                <div class="jumbotron" style="margin-top:80px; background:linear-gradient(skyblue 80%,orangered 20%);"><br>
         <h3><font color=orangered>Signup Please</font></h3>
                    
        <form method="post"  action="" name="myForm">
        <div class="form-group">
            <label><font color=white>Username:</font></label>
            <input type="text" class="form-control" id="id" name="id"  placeholder="UserName..." >
            <div id="iderror" style="color:red;"><?php echo $msg;?></div>
        </div>
       
        <div class="form-group">
            <label><font color=white>PASSWORD:</font></label>
            <input type="password"  class="form-control" id="pwd" name="pwd" placeholder="Password 6 to 10 char only ">
            <div id="pwderror"></div>   
        </div>
       
        <div class="form-group">
            <label><font color=white>CONFIRM PASSWORD:</font></label>
            <input type="password" placeholder="Confirm password..." class="form-control" id="cpwd" name="cpwd">
            <div id="cpwderror"></div>  
        </div>
         
       <button type="button" class="btn btn-primary" onclick="Validate()" >SignUp </button>
            </div>
         </div>
     </div>
      
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>

</html>
php check if username already exist or not in mysql table
The Tech Matin

Hope this tutorial was helpful  J

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database