2014-09-26 94 views
0

我正在为我的公司进行在线aptitude测试,它将从数据库中挑选20个随机问题并将其显示在网页上以供回答。随机挑选变量并将其存储在SQL数据库中

问题是,它无法正常在数据库中存储的值(虽然存储的问题和答案到数据库的越来越混乱了),请任何一个能帮助我解决这个问题,

下面的代码正从候选答案(简单的演示一样拿起只有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 &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q1' value = '$b'></input>$b &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q1' value = '$c'></input>$c &nbsp &nbsp "; 
    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 &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q2' value = '$b'></input>$b &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q2' value = '$c'></input>$c &nbsp &nbsp "; 
    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 &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q3' value = '$b'></input>$b &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer$q3' value = '$c'></input>$c &nbsp &nbsp "; 
    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

你需要获得问题的答案,而POST参数你得到 – Puttu 2014-09-26 06:54:31

+0

如何做到这一点? – Redsun 2014-09-26 06:59:40

+0

在显示的问题你需要添加你的问题编号,同时张贴回你的答案你需要发布问题编号与答案/选项使用数组然后,你可以得到的价值观,并相应插入你的问题。 – Puttu 2014-09-26 07:03:44

回答

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>&nbsp&nbsp"; 
} 

$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> 

正如你所看到的,我真的使用问题编号。请注意,编写此示例代码时,安全性未被考虑。黑客有足够的空间。

+0

嗨thanx优化代码,你的代码显示的问题,但它没有存储在数据库中的值.. .. – Redsun 2014-09-26 09:21:23

相关问题