How can the issue of repetitive Spiel-ID's be addressed in the given PHP code for generating random results in a league?

The issue of repetitive Spiel-ID's can be addressed by checking if a generated Spiel-ID already exists in the database before inserting it. If it does, generate a new random Spiel-ID until a unique one is found. This ensures that each Spiel-ID is unique and avoids duplicates.

// Generate a random Spiel-ID while checking for duplicates
do {
    $spiel_id = mt_rand(100000, 999999); // Generate a random 6-digit Spiel-ID
    $query = "SELECT * FROM results_table WHERE spiel_id = $spiel_id";
    $result = mysqli_query($conn, $query);
} while (mysqli_num_rows($result) > 0);

// Insert the unique Spiel-ID into the database
$query = "INSERT INTO results_table (spiel_id, team1, team2, result) VALUES ('$spiel_id', '$team1', '$team2', '$result')";
mysqli_query($conn, $query);