将无线电输入按钮的值存储到SQL数据库

时间:2014-09-22 10:42:07

标签: php html mysql sql

我正在为我的公司进行在线资质测试,该测试将从数据库中提取20个随机问题并将其显示在网页上进行回答。

但我在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());

}

1 个答案:

答案 0 :(得分: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";