我正在做在线aptitude测试,它将从数据库中挑选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。
因为你没有在任何地方设置它们。 – Darren 2014-10-07 06:20:30
[最好不要使用mysql函数](http://php.net/manual/en/mysqlinfo.api.choosing.php) – vascowhite 2014-10-07 06:27:07
你能告诉我如何显示问题选项。 – Redsun 2014-10-07 06:31:36