我有一个带有单选按钮的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。只想弄清楚如何从表中获取按钮值并存储在groups表的权限列中。
答案 0 :(得分:0)
您可以将广播命名为数组,您可以使用$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';