【问题标题】:Store Radio Button Value from HTML Table to MySQL Table将单选按钮值从 HTML 表存储到 MySQL 表
【发布时间】:2016-02-23 16:35:41
【问题描述】:

我有一个带有单选按钮的 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。只是想弄清楚如何从表中获取按钮值并存储在组表的权限列中。

【问题讨论】:

标签: php mysql


【解决方案1】:

您可以将收音机命名为数组,您可以使用 $member-&gt;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'; 中获得会员密钥

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2013-03-29
    • 2019-11-24
    • 1970-01-01
    • 2021-02-26
    • 2020-03-25
    • 2011-11-24
    相关资源
    最近更新 更多