Q:

Echo user already exists

Here is my code. Everything works perfect. It prevents username duplication. The only problem I'm having is echoing "User already exists" when an already registered user tries to register again.
<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>Registration</title>
 <link rel="stylesheet" href="style2.css" />
  </head>
  <body>
  <?php
   $con = mysqli_connect("localhost","root","","register");

  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 if (isset($_REQUEST['username'])){

     $username = stripslashes($_REQUEST['username']);
     $username = mysqli_real_escape_string($con,$username); 

     $password = stripslashes($_REQUEST['password']);
     $password = mysqli_real_escape_string($con,$password);

     $query = "INSERT into `users` (username, password) 
     VALUES ('$username', '".md5($password)."')";

     $result = mysqli_query($con,$query);

    if($result){
        echo "<div class='form'>
       <h3>You are registered successfully.</h3>
       <br/>Click here to <a href='login.php'>Login</a></div>";
        }
     }else{
  ?>
 <div class="form">
   <h1>Registration</h1>
   <form name="registration" action="" method="post">

   <input type="text" name="username" placeholder="Username" required />
   <input type="password" name="password" placeholder="Password" required />
   <input type="submit" name="submit" value="Register" />
 </form>
  </div>
 <?php } ?>
</body>
</html>

A:

This should work, problem is that u didn't check for username if exists before u inserted another one. U will see in my code i commented it. 
And don't use md5 encryption for password because it's not safe, use password_hash() and password_verify() functions.
<?php

$con = mysqli_connect("localhost", "root", "", "register");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// if is pressed submit button ( Register )
if (isset($_POST['submit']))
{

    $username = stripslashes($_POST['username']);
    $username = mysqli_real_escape_string($con, $username); 

    $password = stripslashes($_POST['password']);
    $password = mysqli_real_escape_string($con, $password);

    // check if username already exists
    $q = mysqli_query($con, "SELECT username FROM users WHERE username = '$username'");
    $user_exists = mysqli_num_rows($q);

    // if number of rows is grateater that 0 it's mean that username exists
    if ($user_exists > 0)
    {
        echo "Username in use, please select another one.";
    }
    else
    {
        // if username don't exists
        $query = "INSERT INTO users (username, password) VALUES ('$username', '".md5($password)."')";

        $result = mysqli_query($con,$query);

        if($result)
        {
          echo "<div class='form'>
                  <h3>You are registered successfully.</h3>
                  <br/>Click here to <a href='login.php'>Login</a>
                </div>";
        } 
    }

}

?>

<!DOCTYPE html>
<html>
  <head>
      <meta charset="utf-8">
      <title>Registration</title>
      <link rel="stylesheet" href="style2.css" />
  </head>
  <body>

  <div class="form">
    <h1>Registration</h1>
      <form name="registration" action="" method="post">
        <input type="text" name="username" placeholder="Username" required />
        <input type="password" name="password" placeholder="Password" required />
        <input type="submit" name="submit" value="Register" />
      </form>
  </div>

</body>
</html>

