title: How to loop to check either the ID is exist or not in mysql database with PHP after a random function's id is generated?
body: <p>I have a database called Customers with Unique ID, so each time insert from php to database I need to generate a new ID for the new customer from a random function but I need to check the id that generated is exists in database or not.</p>
  
  <p>This is the php random function:</p>
  
  <pre><code>function randomDigits($length){
      $digits = "";
      $numbers = range(0,9);
      shuffle($numbers);
      for($i = 0;$i &lt; $length;$i++)
         $digits .= $numbers[$i];
      return $digits;
  }
  </code></pre>
  
  <p>I tried to pass random length like this:</p>
  
  <pre><code>    $cusid = randomDigits(10);
      $generate_customer_id = "";
  
      $query_customer_id = dbQuery("SELECT customer_id FROM customers WHERE customer_id = '$cusid'");
      $count_customer_id = $query_customer_id-&gt;rowCount();
  
      if($count_customer_id == 1)
      {
  
          $generate_customer_id = randomDigits(10);
  
      }
      else
      {
          $generate_customer_id = $cusid;
      }
      $query = dbQuery("INSERT INTO customers(`customer_id`,`username`,`password`,`email`,`first_name`,`last_name`,`dob`,`id_card`,`family_book_id`,`address`,`district`,`province`,`phone`,`mobile`,`father_name`,`mother_name`,`bank_account_no`,`bank_account_name`) VALUES('$generate_customer_id','$username','$password','$email','$firstname','$lastname','$dob','$idcard','$familybook','$address','$district','$province','$homephone','$mobilephone','$fathername','$mothername','$bankno','$bankname')");
  
      if($query == true)
      {
          $insert_referrer = dbQuery("INSERT INTO referrers (`parent_id`, `customer_id`, `category_id`, `created`,`modified`) VALUES(NULL, '$generate_customer_id', 2, NOW(), NOW())");
          if($insert_referrer == true)
          {
              echo "OK";
          }
          else
          {
              echo "There is an error please check your information and try again referrers!";
          }
      }
      else
      {
          echo "There is an error please check your information and try again customers!";
      }
  </code></pre>
  
  <p>But data return from my AJAX function, if return error I need to click the save customer button many times then i will return success and then data successful saved to database.</p>
  
  <p>But I want user to click only one time then data will insert.</p>
  
  <p>How to implement this please help!</p>
  
  <p>EDiT:
  This is AJAX function:</p>
  
  <pre><code>$.ajax({
          type: "POST",
          url: "../ajax/admin/sales/ajaxAddSalesParent.php",
          data: dataString,
          dataType: "html",
          cache: false,
          success: function(responseMessage){
              if(responseMessage == "OK")
              {
                  swal({
                        title: "Success",
                        text: "Your requested has been completed!",
                        type: "success",
                        showCancelButton: false,
                        confirmButtonClass: "btn-success",
                        closeOnConfirm: false
                      },
                      function(isConfirm) {
                        if (isConfirm) {
                          window.location.href = "sales_members.php";
                        } 
                  });
  
              }
              else
              {
                  $.alert({
                      title: '&lt;h3 style="color: red;"&gt;Error!&lt;/h3&gt;',
                      content: responseMessage,
                      confirm: function(){
  
                      }
                  });
              }
          }
      });
  </code></pre>