Check Whether a username is already taken or not with PHP and MySQL 

On some websites, Certain information is meant to be unique. At the end of the database, this information can be set as UNIQUE Index.
But this is not enough. The reason for this is the database will reject the values but will not tell the user what the error is.

In this tutorial, we just show an error to the user when the same error has occurred.

Here is the structure of what we will do in this tutorial:

  • First We save user information in a database table called users_detail.
  • Make a checking condition each time the user submits the other user info. If a username already exists in the database, an error message will be displayed on the form telling the user that the submitted username has already been taken by the other user.
  • We'll perform a checking condition on the email as well.

Now Create a Database Username_Check

Now Create a Table in database user_details which have four columns One user_id field of type INT and the rest of all user_name, user_email, and password all three of type varchar(255).

Now Create three files with your text editor.

  1.  register_user.php
  2.  register_process.php
  3.  styles.css
The register_user.php holds the user registration form.

register_process.php gets all the values submitted from the register user form. 

And styles.css hold all the CSS styling that makes the user registration form elegant and beautiful.

Open all files in a text editor and write the following code into each file.

register_user.php File:

<?php include('register_process.php') ?>
<html>
<head>
  <title>Register</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <form method="post" action="register_user.php" id="register_user">
  <h1>Register</h1>
  <div <?php if (isset($name_error)): ?> class="form_error" <?php endif ?> >
  <input type="text" name="username" placeholder="Username" value="<?php echo $username; ?>">
  <?php if (isset($name_error)): ?>
  <span><?php echo $name_error; ?></span>
  <?php endif ?>
  </div>
  <div <?php if (isset($email_error)): ?> class="form_error" <?php endif ?> >
      <input type="email" name="email" placeholder="Email" value="<?php echo $email; ?>">
      <?php if (isset($email_error)): ?>
      <span><?php echo $email_error; ?></span>
      <?php endif ?>
  </div>
  <div>
  <input type="password"  placeholder="Password" name="password">
  </div>
  <div>
  <button type="submit" name="register" id="reg_btn">Register</button>
  </div>
  </form>
  /body>
</html>

styles.css File:

#register_user h1 {
  text-align: center;
}
body {
  background: #A9D9C3;
}
#register_user {
  width: 37%;
  margin: 100px auto;
  padding-bottom: 30px;
  border: 1px solid #918274;
  border-radius: 5px;
  background: white;
}
#register_user input {
  width: 80%;
  height: 35px;
  margin: 5px 10%;
  font-size: 1.1em;
  padding: 4px;
  font-size: .9em;
}
.form_error span {
  width: 80%;
  height: 35px;
  margin: 3px 10%;
  font-size: 1.1em;
  color: #D83D5A;
}
.form_error input {
  border: 1px solid #D83D5A;
}
#reg_btn {
  height: 35px;
  width: 80%;
  margin: 5px 10%;
  color: white;
  background: #3B5998;
  border: none;
  border-radius: 5px;
}

register_process.php File:

<?php 
  $db = mysqli_connect('localhost', 'root', '', 'Username_Check');
  $username = "";
  $email = "";
  if (isset($_POST['register'])) {
  $username = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $sql_usr = "SELECT * FROM user_details WHERE username='$username'";
  $sql_email = "SELECT * FROM user_details WHERE email='$email'";
  $res_usr = mysqli_query($db, $sql_usr);
  $res_email = mysqli_query($db, $sql_email);
  if (mysqli_num_rows($res_usr) > 0) {
    $name_error = "Sorry... This username already taken by other user...";
  }else if(mysqli_num_rows($res_email) > 0){
    $email_error = "Sorry... This email already taken by other user...";
  }else{
           $query = "INSERT INTO user_details (username, email, password) 
            VALUES ('$username', '$email', '".md5($password)."')";
           $results = mysqli_query($db, $query);
           echo 'Saved!';
           exit();
  }
  }
?>


If the $name_error message is set, then a class is attached to the div element wrapping that particular input field. This class contains the styling that will provide the red border on the input field.

Thanks, Hope you learn something new today.
Check Whether an username is already taken or not with PHP and MySQL | MadeCodingEasy