I have generated a multiple choice question page, where it takes a random question from my qbanktable in my database. The CorrectAnswer to this question is also in the qbanktable. Here is the code for the question.php page
<form action="grades.php" method="post" id="quiz">
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Question, AnswerA, AnswerB, AnswerC, AnswerD, AnswerE, CorrectAnswer FROM qbanktable ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
?>
<h3>
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["Question"];
}
}
?>
</h3>
<p> </p>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerA"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B3">B)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerB"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C3">C)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerC"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D3">D)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerD"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="E" />
<label for="question-1-answers-D3">E)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerE"];
}
}
?>
</label>
</div>
<div>
<input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
</div>
</ol>
<input type="submit" class="hvr-grow" value="SUBMIT" />
</form>
Now after I press the submit button, it will direct me to grades.php where it will analyze the CorrectAnswer:
<?php
$correctAnswer = $_POST['question-1-correct_answer'];
$answer1 = $_POST['question-1-answer'];
if ($answer1 == $correctAnswer) {
echo "<img src=correct.svg";
}
else {
echo "<img src=wrong.svg";
}
?>
I believe now in the grades.php (code above) I am messing something simple. I was wondering what is the correct code to match the CorrectAnswer with the user answer? Thank you
Multiple ways:
Send the question id in a hidden input and requery the database to get the Correct answer, this requires you to edit the query to also get the question ID
<input type="hidden" name="question-id" id="question-id" value="$row["id"]" >
In the grades.php you can then use to requery the db to get all the answers (especially the correct one)
$questionID= $_POST['question-id'];
The second one is easier but less secure, so try the first option first
You could send it using an hidden input type, but as already commented by @khuderm, this is a bad idea, because someone with little skills could see the answer in the source. I don't think anyone would actually try it, but better safe then sorry.
<input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
In the grades.php
you will need something like this:
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$questionID= $_POST['question-id'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CorrectAnswer FROM qbanktable where id = $questionID";
$result = $conn->query($sql);
So after a few modifications, now it works.
1) I had to include the label for the hidden input in php as follows:
<input type="hidden" name="question-id" id="question-id" value= <?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["id"];
}
}
?> >
Now it gets the id row from my table and send it to the grades.php
2) In grades.php the code to finally check the user input with the correct answer from question id provided from the last page:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$questionID= $_POST['question-id'];
$answer1 = $_POST['question-1-answers'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CorrectAnswer FROM qbanktable WHERE id = $questionID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$correct = $row["CorrectAnswer"];
}
}
if ($answer1 == $correct) {
echo "<img src=correct.svg";
}
else {
echo "<img src=wrong.svg";
}
?>
Now it works fine. Special thanks to @davejal