我正在进行在线资质测试,该测试将从数据库中挑选出2个随机问题,并将其显示在网页上进行回答。
下面的代码是从候选人那里获得答案(简单演示只选择2个随机问题)。
$nbQuestion = 2;
$form = '<form id="form1" name="quest" method="POST" action="" >';
$form .= getQuestion("SELECT * FROM `microsoftq` ORDER BY RAND() LIMIT ".$nbQuestion);
$form .= '<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT"></form>';
// Save answer
if (isset($_POST['SUBMIT']))
{
for($i=1;i<=$nbQuestion;$i++){
saveAnswer($i);
}
}
function getQuestion($query){
$question = "";
$i = 1;
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)) {
$question .= "<b>Question:-<br></b>".$row->Question." <br><br>";
$question .= "<input type='hidden' name='q".$i."' value='".$row->QNo."'>";
$question .= "<input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt1."'></input>$a1    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt2."'></input>$b1    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt3."'></input>$c1     <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt4."'></input>$d1 <br><br> ";
$i++;
}
mysql_free_result($result);
}
function saveAnswer($nb){
$qId=$_POST["q".$nb];
if (is_numeric($qId)) {
$query = mysql_query("SELECT * FROM `microsoftq` WHERE QNo=".$qId);
$rows1 = mysql_fetch_array($query);
$ans = $rows1['Ans'];
$opt = $_POST["answer".$qId];
if($ans==$opt)
{
$val="ct";
}
else
{
$val="wg";
}
mysql_query("insert into $username values('$qId','$opt','$val')")
//$username getting from previous page, pls dont worry about it,..
or die(mysql_error());
}
}
我在$ a,$ b,$ c和$ d收到错误的未定义变量。
答案 0 :(得分:4)
您不再需要使用这些变量(基于您之前的一个问题)。而是替换它:
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt1."'></input>$a1    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt2."'></input>$b1    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt3."'></input>$c1     <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt4."'></input>$d1 <br><br> ";
用这个:
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt1."'></input>".$row->Opt1."    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt2."'></input>".$row->Opt2."    <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt3."'></input>".$row->Opt3."     <br>";
$question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt4."'></input>".$row->Opt4." <br><br> ";
我希望这会有所帮助。