Hi friends, I'm developing the online exam application and strucked. below is my code to display the records randomly each time, but the problem is these questions are repeating and also the answer of the next question(that is about to display) is saved as the answer selected for present displayed question whenever I hit the submit button which is totally wrong method.
`
Inline Code Example Here<form action="#" method='post' >
<td class="center"> <?php
$connect = mysqli_connect('localhost', 'root', '', 'exam') or die('Error connecting to MySQL server.');
$i=0;
$result=mysqli_query($connect,"SELECT * FROM addquestions order by RAND() LIMIT 1 ") or die(mysqli_error());
while ($row = mysqli_fetch_array($result) )
{
$question = $row['question'];
$option_a = $row['option_a'];
$option_b =$row['option_b'];
$option_c = $row['option_c'];
$option_d = $row['option_d'];
$answer = $row['answer'];
?>
<p><b>Question :</b> <?php echo $question;
?>
<p>Answers: <input type = "checkbox" name = 'a' value = 'a'/> <?php echo $option_a; ?></p>
<p> <input type = "checkbox" name = 'b' value = 'b'/><?php echo $option_b; ?> </p>
<p> <input type = "checkbox" name = 'c' value = 'c'/> <?php echo $option_c; ?></p>
<p> <input type = "checkbox" name = 'd' value = 'd'/> <?php echo $option_d; ?></p>
<input type="submit" name = "submit" />
<?php
if(isset($_POST['submit']))
{ $n = strcmp($option_a,$answer);
if(isset($_POST['a']))
{
$query = mysqli_query($connect, "INSERT INTO questions(test_id, question, option_a, option_b, option_c, option_d, answer, user_answer)VALUES('$name','$question', '$option_a', '$option_b', '$option_c', '$option_d', '$answer','a')");
echo "<script>alert('Entered Successfully, ')</script>";
}
else if(isset($_POST['b']))
{
$query = mysqli_query($connect, "INSERT INTO questions(test_id, question, option_a, option_b, option_c, option_d, answer, user_answer)VALUES('$name','$question', '$option_a', '$option_b', '$option_c', '$option_d', '$answer','b')");
echo "<script>alert('Entered Successfully, ')</script>";
}
else if(isset($_POST['c']))
{
$query = mysqli_query($connect, "INSERT INTO questions(test_id, question, option_a, option_b, option_c, option_d, answer, user_answer)VALUES('$name','$question', '$option_a', '$option_b', '$option_c', '$option_d', '$answer','c')");
echo "<script>alert('Entered Successfully, ')</script>";
}
else if(isset($_POST['d']))
{
$query = mysqli_query($connect, "INSERT INTO questions(test_id, question, option_a, option_b, option_c, option_d, answer, user_answer)VALUES('$name','$question', '$option_a', '$option_b', '$option_c', '$option_d', '$answer','d')");
echo "<script>alert('Entered Successfully, ')</script>";
}
else
{
$query = mysqli_query($connect, "INSERT INTO questions(test_id, question, option_a, option_b, option_c, option_d, answer, user_answer)VALUES('$name','$question', '$option_a', '$option_b', '$option_c', '$option_d', '$answer','not selected')");
echo "<script>alert('Entered Successfully, ')</script>";
}
}
} ?>
</form>
`