PHP / MySQL:如何在数据库中保存或插入选定的单选按钮?

时间:2012-12-30 16:58:52

标签: php mysql insert radio-button save

我有一张有n行的表。每行都有一个名称[来自Excel文件]和2组单选按钮。我应该如何在数据库中保存或插入所选的单选按钮?

我的数据库结构是:id,name,vote_for_mayor,vote_for_vmayor

这是我的PHP代码。

echo '<form name="electionForm" action="submit.php" method="post" onreset="updateButCount(event);">
<button type="submit" value="Submit">Submit</button>

<table id="election" onclick="updateButCount(event);">
list($cols,) = $xlsx->dimension();
$var=0;
$var1=0;
foreach( $xlsx->rows() as $k => $r) {
    $var++;
    $var1++;
    <tr>
        <td>'.$k.'</td>
        <td>'.( (isset($r[0])) ? $r[0] : '&nbsp;' ).'</td>  //names
        <td><input type = "Radio" Name ="vote'.$var.'" value= "pacada"></td>
        <td><input type = "Radio" Name ="vote'.$var.'" value= "toledo"></td>
        <td><input type = "Radio" Name ="vote1'.$var1.'" value= "apostol"></td>
        <td><input type = "Radio" Name ="vote1'.$var1.'" value= "abdul"></td>
    </tr>
}
</table>
</form>';

1 个答案:

答案 0 :(得分:0)

将HTML更改为:

    <td><input type = "Radio" Name ="vote['.$var.']" value= "pacada"></td>
    <td><input type = "Radio" Name ="vote['.$var.']" value= "toledo"></td>
    <td><input type = "Radio" Name ="vote1['.$var.']" value= "apostol"></td>
    <td><input type = "Radio" Name ="vote1['.$var.']" value= "abdul"></td>

然后在处理表单时,变量$_POST['vote']$_POST['vote1']将是包含每行上单选按钮值的数组。您可以遍历它们并将它们插入数据库。