我正在进行在线资质测试,该测试将从数据库中挑选出2个随机问题,并将其显示在网页上进行回答。
问题在于它没有在数据库中正确存储值(在将问题和答案存储到数据库中时,它会变得混乱而有些甚至不存储)。任何人都可以帮我解决这个问题,
下面的代码是从候选人那里获得答案(简单演示只选择2个随机问题)。
<form id="form1" name="quest" method="POST" action="" >
<?php
$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");
$query = mysql_query("SELECT * FROM `microsoftq` ORDER BY RAND() LIMIT 2 ");
rows1 = mysql_fetch_array($query);
$q1 = $rows1['QNo'];
$qus1 = $rows1['Question'];
$a1 = $rows1['Opt1'];
$b1 = $rows1['Opt2'];
$c1 = $rows1['Opt3'];
$d1 = $rows1['Opt4'];
$ans1 = $rows1['Ans'];
echo " <b>Question:-<br></b>$qus1 <br><br>";
echo " <input type=radio name = 'answer$q1' value = '$a1'></input>$a1    <br>";
echo " <input type=radio name = 'answer$q1' value = '$b1'></input>$b1    <br>";
echo " <input type=radio name = 'answer$q1' value = '$c1'></input>$c1     <br>";
echo " <input type=radio name = 'answer$q1' value = '$d1'></input>$d1 <br><br> ";
$rows2 = mysql_fetch_array($query);
$q2 = $rows2['QNo'];
$qus2 = $rows2['Question'];
$a2 = $rows2['Opt1'];
$b2 = $rows2['Opt2'];
$c2 = $rows2['Opt3'];
$d2 = $rows2['Opt4'];
$ans2 = $rows2['Ans'];
echo " <b>Question:-<br></b>$qus2<br> <br>";
echo " <input type=radio name = 'answer$q2' value = '$a2'></input>$a2    <br>";
echo " <input type=radio name = 'answer$q2' value = '$b2'></input>$b2    <br>";
echo " <input type=radio name = 'answer$q2' value = '$c2'></input>$c2    <br> ";
echo " <input type=radio name = 'answer$q2' value = '$d2'></input>$d2 <br><br> ";
}
?>
<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT">
</form>
下一部分是在用户点击SUBMIT按钮后将它们存储到数据库中。
if (isset($_POST['SUBMIT']))
{
$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");
$opt1=$_POST["answer$q1"]; // Problem is here
$id1=array("$q1","$opt1");
if($ans1==$opt1) // Out Correcting answer part
{
$val1="ct";
}
else
{
$val1="wg";
}
$opt2=$_POST["answer$q2"]; // Problem is here
$id2=array("$q2","$opt2");
if($ans2==$opt2) // Out Correcting answer part
{
$val2="ct";
}
else
{
$val2="wg";
}
mysql_query("insert into $username values('$id1[0]','$id1[1]','$val1')")
or die(mysql_error());
mysql_query("insert into $username values('$id2[0]','$id2[1]','$val2')")
or die(mysql_error());
?>
答案 0 :(得分:0)
我认为您只需要像这样发布问题ID:
<input type="hidden" name="q1" value="'$q1'">
之前
$opt1=$_POST["answer$q1"];
你必须添加:
$q1=$_POST["q1"];
if (is_numeric($q1)) {
$query = mysql_query("SELECT * FROM `microsoftq` WHERE QNo=".$q1);
$rows1 = mysql_fetch_array($query);
$ans1 = $rows1['Ans'];
}
完整的代码
// Build Form
$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')")
or die(mysql_error());
}
}
?>