将单选按钮值从 HTML 表存储到 MySQL 表


Store Radio Button Value from HTML Table to MySQL Table

>我有一个带有单选按钮的HTML表格。该表显示正常,但我不知道如何将所选值放入数据库表中。我正在使用PHP和MYSQL数据库。

下面是表格的代码

<?php
$stmt = $dbh->prepare("SELECT m.member_id , m.username , m.email , g.permission FROM members m INNER JOIN members_groups q 
ON m.member_id = q.member_id INNER JOIN groups g on q.group_id = g.group_id ");
?>
<table>
    <caption>List data from mysql</caption>
    <tr>
      <th class="center"><strong>Name</strong></th>
      <th class="center"><strong>Email</strong></th>
      <th class="center"><strong>Admin</strong></th>
      <th class="center"><strong>Account Manager</strong></th>
      <th class="center"><strong>delete</strong></th>
    </tr>
  <?php
  if($stmt->execute()){
      // output data of each row
      while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
          <tr>
              <td class="center"><?php echo $rows['username']; ?></td>
              <td class="center"><?php echo $rows['email']; ?></td>
              <td class="center">
                    <input type='radio' id='admin' name=<?php echo $rows['username']; ?> value="admin" 
                        <?php echo ($rows['permission']== 31 )?'checked':'' ?>></input> 
              </td>
              <td class="center">
                    <input type='radio' id='ac_manager' name=<?php echo $rows['username']; ?> value="ac_maneger" 
                        <?php echo ($rows['permission']== 1 )?'checked':'' ?> ></input> </td>
              <td class="center" >
                    <a href="javascript:if(confirm('Are you sure you want to delete?'))
                        { location.href='update_account.php?id=<?php echo $rows['member_id']; ?>'; }">delete</a>
              </td>
          </tr>
          <?php
      }
  }
  ?> 
</table>

注意:我尚未创建update_account.php。只是想弄清楚如何从表中获取按钮值并存储在组表的权限列中。

您可以将

收音机命名为数组,也可以使用$member->id作为数组键member[1] member[2]如下所示:

<form method="POST">
<table>
    <tr>
        <td><input type="radio" value="foo" name="member[1]"/>foo</td>
        <td><input type="radio" value="bar" name="member[1]"/>bar</td>
    </tr>
    <tr>
        <td><input type="radio" value="foo" name="member[2]"/>foo</td>
        <td><input type="radio" value="bar" name="member[2]"/>bar</td>
    </tr>
</table>
<button>submit</button>
</form>

当您发帖时,您将获得一个成员密钥$_POST['member'][1] = 'foo';