2015-07-04 69 views
0

如何从while循环获取单选按钮的值?从while循环获取单选按钮值

这是我从数据库中检索数据的代码:

<form action="verify.php" method="POST"> 
<?php 
$con=mysqli_connect("localhost","root","","database"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql = "SELECT * FROM question WHERE ID_Q LIKE 'A%'" ; 
$result = mysqli_query($con,$sql); 
$count=mysqli_num_rows($result); 

$kira=1; 
while($x = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>$kira.</td>"; 
    echo "<th>" . $x['Question']."</th>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td></td>"; 
    echo "<td>"; 
    $sql2 = "SELECT * FROM answer WHERE ID_question ='$x[ID_Q]'" ; 
    $result2 = mysqli_query($con,$sql2); 
    $count2=mysqli_num_rows($result2); 
    while($y = mysqli_fetch_array($result2)){ 
     echo "<input type='radio' name=answer[".$x['ID_Q']."] value=val[".$y['weight_pre']."]>".$y['answer']; 
    echo "<br>"; 
    } 
    echo "</td>"; 
    //echo "<td>".$x['pic']."</td>"; 
    echo "</tr>"; 
    $kira++; 
    } 
?> 
<button type="submit"> Kira </button> 
</form> 

,这是我的“verify.php”

<?php 
session_start(); 
$a=$_GET['name']; 
$b=$_GET['value']; 
$c=$_SESSION['ID_user']; 

if(!isset ($_SESSION['ID_user'])){ 
    echo "You have to log in to be able to view this page."; 
    echo "<meta http-equiv='Refresh' content='1;index.php'>"; 
    die(); 
} 
else { 
    // Create connection 
$con=mysqli_connect("localhost","root","","database"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$s=0; 
while ($s<=20){ 
    $sql = "INSERT INTO answer_user(ID_user, ID_question, answer, 
weight) "."VALUES ('$c','$a','','$b')"; 
$result = mysqli_query($con,$sql); 
$s++; 
} 

} 
?> 

那么,如何才能得到单选按钮的值,一旦用户提交表格?谢谢。

+0

单选按钮的所有属性都需要引号。您从不关闭输入元素。该元素中的问号是什么?不要直接在你的SQL中传递用户输入,使用预准备语句,http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85

+0

抱歉。该问号是某种错字,并感谢您的建议。 :) – kudos

回答

0

您需要使用这样的:

$_POST['radio']; 

替换“广播”与你无论你的无线输入的名称。

+0

我试过了。并且它仍然不起作用。它只是插入ID而不是数据。 – kudos

+0

这样做。 '$ radio = $ _ REQUEST ['radio'];'然后在需要的地方拨打$ radio。如果这不起作用,我想我知道为什么。 – sam44

+0

我已经得到了值(显示在url中),但它仍然没有存储在数据库中。 :( – kudos