通过 PHP 中选中的单选按钮检查数据库中的正确答案 [英] Checking the correct answer from the database by a checked radio button in PHP
问题描述
我正在创建在线评估.我只在一页中随机显示所有问题.我很难在数据库中检查已选中单选按钮的正确答案.我不知道该做什么以及如何做的逻辑.
这是我随机显示问题的php代码,
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
这是我的html代码和php代码,
<?php$i++;}?><center><button id='next<?php echo $i;?>'class='next btn btn-success' name="finish" type='submit'>Finish</button></center></表单>
表名:问题
表格字段:QUESTION_NO、QUESTION、ANSWER_1、ANSWER_2、ANSWER_3、ANSWER_4、ANSWER
将 Answerquestions 设置为单选按钮的值
<input type="radio" value="ANSWER2" name="name">
在php代码中检查使用正确答案提交的值
从db获取正确答案并将其保存在var中
$query "SELECT correct_answer FROM TABEL_NAME";
例如在 var Correct_a 中获取查询
$user_answer = $_POST['name'];如果($user_answer == $correct_a)返回真别的返回假
I am creating an Online Assessment. I display all the questions randomly in a one page only. I have difficulty on how to check the correct answer in the database for checked radio button. I don't know what to do and the logic on how to do it.
This is my php codes for displaying the questions randomly,
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
This is my html codes with php codes,
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="1" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="2" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="3" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="4" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
Table name: questions
Table fields: QUESTION_NO, QUESTION, ANSWER_1, ANSWER_2, ANSWER_3, ANSWER_4, ANSWER
set Answerquestions to value for radio button
<input type="radio" value="ANSWER1" name="name">
<input type="radio" value="ANSWER2" name="name">
in php code check the value submited with the correct Answer
get the correct Answer from db and save it in var
$query "SELECT correct_answer FROM TABEL_NAME";
and fetch query in var correct_a for example
$user_answer = $_POST['name'];
if($user_answer == $correct_a)
return true
else
return false
这篇关于通过 PHP 中选中的单选按钮检查数据库中的正确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!