title: Insert data (only 1 time) in while loop using PHP
body: <p>I have the PHP code as below:</p>
  
  <pre><code>&lt;?php
      if(isset($_POST["tbn_submit"])){
          $userName = $_POST["text_username"];
          $pass = $_POST["text_password"];
          //$sql = "SELECT * FROM tbluser WHERE username='".$userName."' AND password='".$pass."'";
          $sql = "SELECT * FROM tbluser";
          $res = mysql_query($sql) or die(mysql_error());
          if(mysql_num_rows($res)&gt;0){
              while($row= mysql_fetch_array($res)){
                  $username=$row['username'];
                  $userpas = $row['password'];
                  $user_id=$row['userId'];
                  $user_role=$row['possition'];
                  $_SESSION['username'] = $username;
                  $_SESSION['uid'] = $user_id;
                  if($userName == $username &amp;&amp; $pass == $userpas){
                      if($user_role=="Admin"){
                          echo'&lt;script&gt;window.location="admin_project.php?uid='.$user_id.'";&lt;/script&gt;';
  
                      }else{
                          echo'&lt;script&gt;window.location="user_project.php?uid='.$user_id.'";&lt;/script&gt;';
                      }
                  }
                  else if($userName == $username &amp;&amp; $pass != $userpas){
                      echo "&lt;span style='text-align:center;color:red;'&gt;Wrong password.&lt;/span&gt;";
                  }
                  else if($userName != $username &amp;&amp; $pass != $userpas){
  //In this point I got insert multi time so I want it insert only 1 time to database
                      $query = "INSERT INTO tbluser(userId,username,password,possition,user_status) VALUES('','".$userName."','".$pass."','',1)";
                      $result = mysql_query($query) or die(mysql_error());
                      $id = mysql_insert_id();
                      if($result){
                          echo'&lt;script&gt;window.location="user_project.php?uid='.$user_id.'";&lt;/script&gt;';
                      }
                  }
              }
          }else {
              echo "re";
  
          }
      }
  ?&gt;
  </code></pre>
  
  <p>This is my login page submission. When the user inputs their username and password, if the username and password are already in the database it will go to test some case like in code, but if the username is in the database but the password does not match it should display <code>wrong password.</code>. </p>
  
  <p>If the username and password don't exist in the database, the program should create username and password and go to other page. I have an error with this last case - I have inserted a lot of records in the database with the same data. I know it's because I wrote these entries in a while loop in my code but I don't know any other way of doing this. How can I populate my database with individual records and not write duplicate entries in my <code>while</code> loop?</p>