PHP/MySQL:如何在数据库中保存或插入选定的单选按钮? [英] PHP/MySQL: How to save or insert the selected radio buttons in a database?
本文介绍了PHP/MySQL:如何在数据库中保存或插入选定的单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含n行的表.每行都有一个名称(来自Excel文件)和2套单选按钮.我应该如何在数据库中保存或插入选定的单选按钮?
I have a table having n rows. Each row has a name [came from an Excel file] and 2 sets of radio buttons. How should I save or insert the selected radio buttons in a database?
我的数据库的结构是:id,名称,vote_for_mayor,vote_for_vmayor
The structure of my database is: 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] : ' ' ).'</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>';
推荐答案
将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']
将是包含每行单选按钮值的数组.您可以遍历它们并将其插入数据库.
Then when processing the form, the variables $_POST['vote']
and $_POST['vote1']
will be arrays containing the value of the radio button on each row. You can loop through them and insert them into the database.
这篇关于PHP/MySQL:如何在数据库中保存或插入选定的单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文