I have this section of a form I need for my customer signup sheet I am creating.
My goal is check if the customer exists, and if they do, output the customerid into an input box. If the customer does not exist, a 6 digit number should be generated in the input box instead.
Here my code:
if(!empty($row['customerid'])) {
echo '<input class="input-xlarge focused" disabled id="focusedInput" name="customerID" value="'.$row['customerid'].'">';
} else {
$six_digit_random_number = mt_rand(100000, 999999);
$sql = "SELECT * FROM customers WHERE customerid='$six_digit_random_number'";
$loop = 0;
while($row = $result->fetch_assoc()){
if($loop == 10) {
echo "CANNOT GENERATE RANDOM NUMBER";
die();
}
$six_digit_random_number = mt_rand(100000, 999999);
$loop++;
}
echo '<input class="input-xlarge focused" disabled id="focusedInput" name="customerID" value="'.$six_digit_random_number.'">';
}
My question is, will the while loop actually stop duplicates from being made, or is there something I am overlooking?
--UPDATE
I switched out my original $six_digit_random_number
with a number that already exists in the database, and the while loop did not change the number at all.
So I guess my updated question is, how can I check if the number has been used before?