title: How to prevent duplicate usernames when people register using Php?
body: <p>I have been making a login/register system.The only problem I am running into is how to make it so that users cannot register with duplicated usernames. I have looked around and have seen many examples of it but my code is not working.</p>
  
  <p>I wanted to prevent users from having the same username when they register on my page. Any help is appreciated.</p>
  
  <p><strong>MySQL Table:</strong></p>
  
  <pre><code>Create_User:
  ID  |  upload_img |   fullname   |  username  |   role   | password    
  1   |  &lt;img&gt;      |   Billy      |  billyuser |   User   | test123
  2   |  &lt;img&gt;      |   Admin One  |  adminone  |   Admin  | adminonpass
  </code></pre>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;form action="create_user.php" method="post" enctype="multipart/form-data"&gt;
  
         Profile Photo (Optional)&lt;br&gt;
         (Max file size:2MB, file type:jpeg, jpg, png)&lt;br&gt;&lt;br&gt;
         &lt;input type="file" name="fileToUpload" id="fileToUpload" required/&gt;&lt;br&gt;&lt;br&gt;
         Full Name: &amp;nbsp;
         &lt;input type="text" name="fname" id="fname" size="40" maxlength="70" placeholder="Please enter your full name"&gt;&lt;br&gt;&lt;br&gt;
         Username: &amp;nbsp;
         &lt;input type="text" name="uname" id="uname" size="35" maxlength="62" placeholder="Please enter your username"&gt;&lt;br&gt;&lt;br&gt;
         Role: &amp;nbsp;
         Customer&lt;input type="radio" name="role" id="role" value="Customer"&gt;
         Administrator&lt;input type="radio" name="role" id="role" value="Admin"&gt;&lt;br&gt;&lt;br&gt;           
  
           Password: &lt;br&gt;
           &lt;input type="password" name="pword" id="pword" size="35" pattern=".{6,}" placeholder="Password" title="Six or more characters"&gt;&lt;br&gt;&lt;br&gt;
           &lt;input type="password" name="cfmpword" id="cfmpword" size="35" placeholder="Confirm Password"&gt;&lt;br&gt;&lt;br&gt;
  
  
           &lt;button type="submit" name="signbtn" style="border:none; background:white; padding:0px"&gt;
              &lt;img src="button/Sign%20up.png"&gt;
           &lt;/button&gt;&lt;br&gt;
           &lt;a href="signin.html"&gt;Back to Sign In&lt;/a&gt;
  
    &lt;/form&gt;
  </code></pre>
  
  <p><strong>Php:</strong></p>
  
  <pre><code>&lt;?php
  
  if (isset($_FILES["fileToUpload"]["name"])) {
  
        $file=$_FILES["fileToUpload"]["name"];
        $target="uploadfile/" . $file;
        $fname=$_POST['fname'];
        $uname=$_POST['uname'];
        $role=$_POST['role'];
        $pword=$_POST['pword'];
  
        $conn=mysqli_connect("localhost", "root", "" , "SportFacility");
  
        $sql_insert = "INSERT into create_user (upload_img, fullname, username, role,  password) 
        values ('$file', '$fname' , '$uname' , '$role' ,  '$pword' )";
  
        $result=mysqli_query($conn, $sql_insert);
  
        $allowedType=array("image/jpeg", "image/jpg", "image/png");
        if(in_array ($_FILES["fileToUpload"]["type"] ,$allowedType))
        {
             echo "&lt;script type='text/jscript'&gt;alert('File type is acceptable')&lt;/script&gt;";
        }
        else
        {
            echo "&lt;script type='text/jscript'&gt;alert('Invalid file type')&lt;/script&gt;";
            exit();
        }
        if($_FILES["fileToUpload"]["size"] &lt; 2000000)
        { 
            echo "&lt;script type='text/jscript'&gt;alert('File size is acceptable')&lt;/script&gt;";
        }
        else
        {
            echo "&lt;script type='text/jscript'&gt;alert('File is too large')&lt;/script&gt;";
            exit();
        }
        $directoryfile=move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
  
        if($result)
        {
            header("Location: login.html");
        }
        else
        {
            header("Location: register.html");
        }
  
  mysqli_close($conn);
  
  }
  
   if (isset($_POST['uname']))
        $uname=$_POST['uname'];
        $conn=mysqli_connect("localhost", "root", "" , "SportFacility");
        $sqluser="SELECT username FROM create_user WHERE username='$uname' ";
        $qresult=mysqli_query($conn, $sqluser);
        $count=mysqli_num_rows($qresult);
        if($count)
        {
             echo "Username is already taken";
        }
        else
        { }
   ?&gt;
  </code></pre>