单选按钮和检查用户在测验中的选择

时间:2013-01-22 14:43:52

标签: mysql phpmyadmin radio-button multiple-choice

我正在制作一个测验系统,尝试了另一种从MySQL获取变量和值的方法。

$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
$questionid = 0;
for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' . $i .'" value="' . $questionid . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer4'] . '<br />';
$questionid++;
}

现在,我想让这个人选择正确的答案但是当我尝试选择问题1中的答案,然后问题2时,它不会让我感到可能因为收音机具有相同的名称 - 我不喜欢我知道如何让学生在每个问题中选择一个答案,以及如何做出选择(将其存储在变量中并检查答案是否正确)。

2 个答案:

答案 0 :(得分:0)

你有PHP变量和引号('/“)的问题。连接运算符没有正确使用。

  

name =“'。$ i。'”value =“'。$ questionid。'”

       /\ /\       /\         /\

参见代码:

for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
$questionid++;
}

答案 1 :(得分:0)

我认为你的做法并不好。我认为迭代代码的最佳方法是while()loop.Like

$all = mysql_query("SELECT * FROM 'questions'");
while($all_array=mysql_fetch_array($all))
{
$question = mysql_query("SELECT * FROM questions WHERE id='".$all_array['id']."'");
while($stat=mysql_fetch_array(question)){
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
}
}
相关问题