PHP/MySQL: How to save or insert the selected radio buttons in a database? [closed]

☆樱花仙子☆ 提交于 2019-12-14 03:28:56

问题


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?

The structure of my database is: id, name, vote_for_mayor, vote_for_vmayor

Here's my php code.

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:


Change the HTML to:

    <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>

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.



来源:https://stackoverflow.com/questions/14092608/php-mysql-how-to-save-or-insert-the-selected-radio-buttons-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!