I have a hurtle I just can't figure out.
I want my code to:
- Create a random number
- Test if it exist in the database
- If it exist, create a new random number og do number 2. again.
- If it don't exist, create it in the database.
I've tried the following code, which I found in a similar problem on Stackoverflow, but I just can't figure this out:
$key = true;
while($key){
$unique_number_new = rand(1000,9999);
$result = mysql_query("SELECT * FROM brugere WHERE medlemsnummer = $unique_number_new");
if(mysql_num_rows($result) > 0) {
$key = false;
// Allready exists
} else {
$key = true;
// Create in database
}
echo "Code done! - " . $unique_number_new;
}
The problem is, it gives me multiple numbers (see https://www.dance4fun.dk/test_folder/number_test.php)
How can I create a code that does the 4 above mentioned, steps?
Hope you can help me out with this!