gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:24:15 25 4
gpt4 key购买 nike

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

最佳答案

您可以将 radio 命名为数组,并且可以使用 $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'; 中获得成员 key

关于php - 将单选按钮值从 HTML 表存储到 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35583038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com