Saat Anda mengajukan pertanyaan kepada pengguna, sebuah pertanyaan dipilih secara acak dari database.
Kemudian, pengguna mengirimkan formulir Anda, pertanyaan lain dipilih secara acak, dan itulah pertanyaan yang Anda gunakan untuk memeriksa jawabannya, bukan pertanyaan yang Anda ajukan kepada pengguna.
Anda perlu menambahkan input tersembunyi di formulir Anda, yang berisi id pertanyaan
<input type="hidden" name="question_id" value="<?php echo $question_id ?>" />
Dan kemudian ketika Anda memeriksa jawabannya, pastikan untuk mengambil pertanyaan yang tepat dari database
Kodenya akan terlihat seperti ini
<?php
// Check user answer for previous question
if (isset($_POST['submit'])) {
// Retrieve the id of the question you asked
$previous_question_id = (int) $_POST['question_id']; // cast to integer to prevent sql injection.
// Query database
$get_question = "SELECT * from questions_table where id = $previous_question_id";
$result_get_question = mysqli_query($conn, $get_question);
$row_get_question = mysqli_fetch_array($result_get_question);
// Assign database response to variables
$correct = $row_get_question['correct'];
$selected_radio = $_POST['response'];
if ($selected_radio == $correct)
echo "THAT ANSWER IS CORRECT";
else
echo "THAT ANSWER IS WRONG!";
}
// Load new question to ask to the user
$get_question = "SELECT * from questions_table order by rand() limit 1";
$result_get_question = mysqli_query($conn,$get_question);
$row_get_question = mysqli_fetch_array($result_get_question);
// assign thing we want to print in the template to variable
$question_id = $row_get_question['question_id'];
$question = $row_get_question['question'];
$a1 = $row_get_question['a1'];
$a2 = $row_get_question['a2'];
$a3 = $row_get_question['a3'];
$a4 = $row_get_question['a4'];
?>
<PASTE YOUR TEMPLATE HERE>