如何从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++;
}
}
?>
那么,如何才能得到单选按钮的值,一旦用户提交表格?谢谢。
单选按钮的所有属性都需要引号。您从不关闭输入元素。该元素中的问号是什么?不要直接在你的SQL中传递用户输入,使用预准备语句,http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85
抱歉。该问号是某种错字,并感谢您的建议。 :) – kudos