我正在为我的公司进行在线资质测试,该测试将从数据库中提取20个随机问题并将其显示在网页上进行回答。
问题是,它没有在数据库中正确存储值(在将问题和答案存储到数据库中时会混淆),请任何人帮我解决这个问题,
下面的代码是从候选人那里获得答案(简单演示,只需要提取3个随机问题),..
<form id="form1" name="quest" method="POST" action="" style="margin-left:60px;">
<?php
$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");
$query = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 3 ");
$rows = mysql_fetch_array($query);
$q1 = $rows['QNo'];
$qus1 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];
echo " <b>Question:-<br></b>$qus1 <br>";
echo " <input type=radio name = 'answer$q1' value = '$a'></input>$a    ";
echo " <input type=radio name = 'answer$q1' value = '$b'></input>$b    ";
echo " <input type=radio name = 'answer$q1' value = '$c'></input>$c     ";
echo " <input type=radio name = 'answer$q1' value = '$d'></input>$d <br><br> ";
$rows = mysql_fetch_array($query);
$q2 = $rows['QNo'];
$qus2 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];
echo " <b>Question:-<br></b>$qus2 <br>";
echo " <input type=radio name = 'answer$q2' value = '$a'></input>$a    ";
echo " <input type=radio name = 'answer$q2' value = '$b'></input>$b    ";
echo " <input type=radio name = 'answer$q2' value = '$c'></input>$c     ";
echo " <input type=radio name = 'answer$q2' value = '$d'></input>$d <br><br> ";
$rows = mysql_fetch_array($query);
$q3 = $rows['QNo'];
$qus3 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];
echo " <b>Question:-<br></b>$qus3 <br>";
echo " <input type=radio name = 'answer$q3' value = '$a'></input>$a    ";
echo " <input type=radio name = 'answer$q3' value = '$b'></input>$b    ";
echo " <input type=radio name = 'answer$q3' value = '$c'></input>$c     ";
echo " <input type=radio name = 'answer$q3' value = '$d'></input>$d <br><br> ";
?>
<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT">
</form>
下一部分存储到数据库中,..
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_POST['SUBMIT']))
{
$opt1=$_POST["answer1"];
$opt2=$_POST["answer2"];
$opt3=$_POST["answer3"];
$username=$_GET['username']; // getting this value from last webpage pls dont worry about this
$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");
mysql_query("insert into $username values('$qus1','$opt1')")
or die(mysql_error());
mysql_query("insert into $username values('$qus2','$opt2')")
or die(mysql_error());
mysql_query("insert into $username values('$qus3','$opt3')")
or die(mysql_error());
print "<script>window.close('techtest.php'); window.location = \"final.html\";</script>";
}
?>
答案 0 :(得分:0)
请尽量不要重复代码。所以重写它是这样的:
<form id="form1" name="quest" method="POST" action="" style="margin-left:60px;">
<?php
function input_option($QNo,$Opt)
{
echo "<input type=radio name='answer$QNo' value='$Opt'>$Opt</input>  ";
}
$count = 3;
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$sel = mysql_select_db("demo");
$query = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT $count");
while ($row = mysql_fetch_assoc($query))
{
extract($row);
echo "<b>Question:-<br></b>$Question <br>".
input_option($QNo,$Opt1).
input_option($QNo,$Opt2).
input_option($QNo,$Opt3).
input_option($QNo,$Opt4);
}
?>
<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT">
</form>
现在我没有看到该页面有任何其他错误,所以让我们看看另一段代码。我不认为我们知道问题的数量,记住数据库中有多个问题,你随机选择三个。他们可以有任何数字。所以下面的代码必须解决这个问题:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if (isset($_POST['SUBMIT']))
{
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$sel = mysql_select_db("demo");
$username = $_GET['username'];
foreach ($_POST as $post)
if (substr($post,0,6) == 'answer')
{
$question = substr($post,6);
$option = ${$post};
$sql = "insert into $username values('$question','$option')";
mysql_query($sql) or die(mysql_error());
}
}
?>
<script>
window.close('techtest.php');
window.location = \"final.html\";
</script>
如你所见,我真的使用了问题编号。请注意,编写此示例代码时不考虑安全性。有足够的空间来攻击这个。