验证用户名不起作用

I have written this code , its working to add users but for duplicate user it again saved the value os same user name. i wanted to give popup message if username already taken. i m beginner please help.

  <?php
 ob_start();
  include("db.php");
  if(isset($_POST['send'])!="") {
  $username = mysqli_real_escape_string($con, $_POST['username']);
  $usermail = mysqli_real_escape_string($con, $_POST['usermail']);
  $usermobile = mysqli_real_escape_string($con, $_POST['usermobile']);
  $bool = true;
  $con = mysqli_connect("localhost","root","","demo");
  $result = mysqli_query("SELECT `username` FROM `sample` WHERE username = '$username'");
  if(mysqli_num_rows($result)>0)
  {
          Print '<script>alert("Username has been taken!");</script>'; 

      }
        if ($bool) {
        $inssql = "insert into sample set 
        username = '" . $username . "',
        emailid = '" . $usermail . "',
        mobileno = '" . $usermobile . "',
        created = now()";
        $update = mysqli_query($con, $inssql);


        }
    }

Make sure you finish the script or turn off your flag before making the insert:

if(mysqli_num_rows($result)>0)
{
    Print '<script>alert("Username has been taken!");</script>';
    die('username already taken');
    //$bool = FALSE;
}

If you still having duplicate entries, debug what is the result of $username and compare it with the value in the database.