php - A multiple choice is pulled randomly from mysql database. Use the correct answer on the grade page -
i have generated multiple choice question page, takes random question qbanktable in database. correctanswer question in qbanktable. here code 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 qbanktable order 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 press submit button, direct me grades.php analyze 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 in grades.php (code above) messing simple. wondering correct code match correctanswer user answer? thank you
multiple ways:
send question id in hidden input , requery database correct answer, requires edit query question id
<input type="hidden" name="question-id" id="question-id" value="$row["id"]" >
in grades.php can use requery db answers (especially correct one)
$questionid= $_post['question-id'];
the second 1 easier less secure, try first option first
you send using hidden input type, commented @khuderm, bad idea, because little skills see answer in source. don't think try it, better safe sorry.
<input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["correctanswer"]" >
in grades.php
need 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 qbanktable id = $questionid"; $result = $conn->query($sql);
Comments
Post a Comment