title: Prevent users from registering with same username and e-mail
body: <p>I am trying to make a simple register page where users register with username, email, password and gender. I somehow stumbled over some problems, the code I made seems to let users register with a username or e-mail that already exists. I tried to prevent this in my code but it doesn't seem like it worked. </p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here is the whole code:</p>
  
  <pre><code>&lt;?php
  session_start();
  if (isset($_SESSION['user'])!="") {
      header("Location: index.php");
  }
  include 'includes/config.php';
  
  if(isset($_POST['btn-signup'])) {
  
      $username = strip_tags($_POST['username']);
      $username = strtolower($_POST['username']);
      $email = filter_var($_POST['email'],FILTER_SANITIZE_EMAIL);
      $email = filter_var($email,FILTER_VALIDATE_EMAIL);
      $email = strip_tags($_POST['email']);
      $password = strip_tags($_POST['password']);
      $current_time = strtotime("now");
      $username = $con-&gt;real_escape_string($username);
      $gender = $con-&gt;real_escape_string($_POST["gender"]);
      $email = $con-&gt;real_escape_string($email);
      $password = $con-&gt;real_escape_string($password);
  
      $hashed_password = password_hash($password, PASSWORD_DEFAULT); // this function works only in PHP 5.5 or latest version
  
      $check_username = $con-&gt;query("SELECT * FROM users WHERE username='$username' LIMIT 1");
      $check_email = $con-&gt;query("SELECT * FROM users WHERE email='$email' LIMIT 1");
  
          if(count($_POST)&gt;0) {
  
      if(!isset($msg)) {
      }
      if($_POST['username'] === $check_username){ 
      $msg = 'Username already exists&lt;br&gt;'; 
      }
  
      if($_POST['email'] === $check_email){ 
      $msg = 'Email already exists&lt;br&gt;'; 
      }    
  
      if($_POST['password'] != $_POST['confirm_password']){ 
      $msg = 'Password doesnt match&lt;br&gt;'; 
      }
  
  
      if(!isset($msg)) {
      if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
      $msg = "Invalid e-mail";
      }
      }
  
      if(!isset($msg)) {
      if(!isset($_POST["gender"])) {
      $msg = " Gender field is required";
      }
      }
      }
  
      if(!isset($msg)) {
          $query = "INSERT INTO users(username,email,password,gender,joined) VALUES('$username','$email','$hashed_password','$gender','$current_time')";
                  if(mysqli_query($con, $query)){
                  $msg = "You have registered successfully!"; 
                  } else{
                  $msg = "Could not register your account. Try Again!";
                  }
      }
  }
      $con-&gt;close();
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type"&gt;
      &lt;title&gt;Register&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="signin-form"&gt;
          &lt;div class="container"&gt;
              &lt;form class="form-signin" id="register-form" method="post" name="register-form"&gt;
                  &lt;h2 class="form-signin-heading"&gt;Sign Up&lt;/h2&gt;
                  &lt;hr&gt;
                  &lt;?php
                  if (isset($msg)) {
                  echo $msg;
                   }
                   ?&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control" name="username" placeholder="Username" required="" type="text"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control" name="email" placeholder="Email address" required="" type="email"&gt; &lt;span id="check-e"&gt;&lt;/span&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control" name="password" placeholder="Password" required="" type="password"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control" name="confirm_password" placeholder="Confirm password" required="" type="password"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;select class="form-control" id="gender" name="gender"&gt;
                          &lt;option disabled hidden="" selected&gt;
                              Select
                          &lt;/option&gt;
                          &lt;option&gt;
                              Male
                          &lt;/option&gt;
                          &lt;option&gt;
                              Female
                          &lt;/option&gt;
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;hr&gt;
                  &lt;div class="form-group"&gt;
                      &lt;button class="btn btn-default" name="btn-signup" type="submit"&gt;&lt;span class="glyphicon glyphicon-log-in"&gt;&lt;/span&gt; &amp;nbsp; Create Account&lt;/button&gt; &lt;a class="btn btn-default" href="index.php" style="float:right;"&gt;Log In Here&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>