2014-09-22 57 views
-1

我正在爲我的公司進行在線能力測試,它將從數據庫中挑選20個隨機問題並將其顯示在網頁上以供回答。將無線電輸入按鈕的值存儲到SQL數據庫

但我有在SQL數據庫存儲回答值問題,請任何一個能幫助我解決這個問題,

<?php 


    $connect = mysql_connect("localhost","root","") 
    or die(mysql_error()); 
    $sel=mysql_select_db("aptitude"); 


$query = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 20 "); 

while($rows = mysql_fetch_array($query)){ 

    $q = $rows['QNo']; 
    $qus = $rows['Question']; 
    $a = $rows['Opt1']; 
    $b = $rows['Opt2']; 
    $c = $rows['Opt3']; 
    $d = $rows['Opt4']; 
    $ans = $rows['Ans']; 


    echo "<b>Question:-<br></b>$qus <br>"; 
    echo " <input type=radio name = 'answer[$q]' value = '$a'></input>$a &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer[$q]' value = '$b'></input>$b &nbsp &nbsp"; 
    echo " <input type=radio name = 'answer[$q]' value = '$c'></input>$c &nbsp &nbsp "; 
    echo " <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> "; 


} 

?> 

,但我的用戶點擊後試圖存儲值上提交按鈕: -

if (isset($_POST['SUBMIT'])) 
{ 

$username=$_GET['username']; 

$opt1=$_POST["answer1"]; 
$opt2=$_POST["answer2"]; 

mysql_query("insert into $username values('Q1','$answer1')") 
or die(mysql_error()); 
mysql_query("insert into $username values('Q2','$answer2')") 
or die(mysql_error()); 

} 
+0

你在$ _POST什麼陣列[「答案「],提交表單後? – Khushboo 2014-09-22 10:43:41

+0

請參閱更新的問題,... – 2014-09-22 10:53:06

回答

1

雖然爲輸入[type ='radio']賦值名稱值,您的值爲'answer [1]',而在提交表單時,您將通過$ _POST ['answer1']獲取值。要麼改變它,而分配值到name屬性或改變它,而得到它通過POST變量即:

要麼

$_POST['answer[1]'] 

echo " <input type=radio name = 'answer$q' value = '$a'></input>$a &nbsp &nbsp"; 
+0

這一個也是行不通的,... – 2014-09-23 04:13:56

+0

$ opt1 = $ _ POST [「answer [1]」]; $ OPT2 = $ _ POST [ 「回答[2]」]; ,....我正在使用像這樣,是正確的方法來獲取值,.. – 2014-09-23 04:21:07

+0

雅在名稱屬性值中使用方括號存在問題。 PHP默認將這些字符轉換爲下劃線。以下是可幫助您的鏈接: [鏈接](http://php.net/manual/en/language.variables.external.php) – 2014-09-23 05:21:03

相關問題