php check if username name 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.







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


$msg="This username already in use please choose different one...";
     $query=mysqli_query($connect,"INSERT INTO signup VALUES('$id','$pwd')");
     $_SESSION['username'] = $id;
 echo "<script>


<!DOCTYPE html>
<html lang="en">   
    <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 -->

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
      <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="">
  <body >
      <!--JavaScript Starts-->
    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
          if(id.value.length<6  || id.value.length > 20){
             iderror.innerHTML="Username Must be 6 To 20 Character Long";
         else if(pwd.value.length<6 || pwd.value.length >15){
             pwderror.innerHTML="Password Must Be 10 To 15 Character Long";
         else if(cpwd.value!=pwd.value){
           cpwderror.innerHTML="Password Mismatch";
      <!--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 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 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>  
       <button type="button" class="btn btn-primary" onclick="Validate()" >SignUp </button>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src=""></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

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