如何将在线考试系统的答案存储到数据库中

时间:2014-06-28 07:23:02

标签: php mysql

我正在创建一个在线考试系统,从数据库中获取问题和答案,并在单选按钮中显示选项。但是,问题是如果我点击保存它只是保存一个答案而不是从所有问题回答

提取问题的php代码

 $quer=" SELECT * FROM questionset ORDER BY RAND() ";
 $query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
  {
  $na= $row['qus'];
  $opa= $row['opa'];
   $opb= $row['opb'];
  $opc= $row['opc'];
    $opd= $row['opd'];

 echo "<div class='pr3'>";

echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans' value='" . $opa. "' onclick='answ(this.value)' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opb. "' onclick='answ(this.value)'>";
echo $opb;
echo "<br/>";
    echo "<input type='radio' name='ans' value='" . $opc. "' onclick='answ(this.value)'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opd. "' onclick='answ(this.value)'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval' name='ansval' >";
    echo "</form>";

 echo " </div>";

  }

只更新一个文本框的Javascript

<script>
function answ(answer) {
document.getElementById("ansval").value = answer;
}
</script>

2 个答案:

答案 0 :(得分:1)

所有问题的单选按钮名称都相同。您必须为每个答案集动态制作单选按钮名称。您可以使用以下代码:

$quer=" SELECT * FROM questionset ORDER BY RAND() ";
 $query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
  {
  $questionId = $row['questionId']; //primary key of question table
  $na= $row['qus'];
  $opa= $row['opa'];
   $opb= $row['opb'];
  $opc= $row['opc'];
    $opd= $row['opd'];

 echo "<div class='pr3'>";

echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opa. "' onclick='answ(this.value,'<?php echo $questionId; ?>')' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opb. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opb;
echo "<br/>";
    echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opc. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opd. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval<?php echo $questionId; ?>' name='ansval<?php echo $questionId; ?>' >";
    echo "</form>";

 echo " </div>";

  } 

你的javascript函数看起来像:

<script>
function answ(answer,qId) {
document.getElementById("ansval"+qId).value = answer;
}
</script>

在回答提交后,您可以以相同的方式获取提交的值。即$ _POST [&#39; ansval&#39;。$ questionId];在获得值之前,您必须创建另一个循环来获取所有问题ID。

答案 1 :(得分:0)

ID字段必须是唯一的     ++ $计数器;     echo“”;

$_POST['ansval1'],$_POST['ansval2'],$_POST['ansval3'].....