gpt4 book ai didi

php - foreach 循环中的复选框值获取不起作用

转载 作者:行者123 更新时间:2023-11-29 10:04:49 25 4
gpt4 key购买 nike

使用下面给出的代码,我尝试从 MySQL 数据库获取复选框的数据,但它不起作用,并且仅获取最后检查的值。

使用 var_dump($checked); 给出正确的结果。

PHP 脚本

$group_id = $_POST['group_id'];
$team_id = $_POST['team_id'];
if(!empty($_POST['team_id'])) {
foreach($_POST['team_id'] as $checked) {
// var_dump($checked);
$sql = $db->prepare("SELECT a.team, a.player, a.result, a.note, b.category FROM teams a INNER JOIN groups b ON a.team_id = b.id WHERE a.team_id = :team_id_id AND a.group_id = :group_id ");
$sql->execute(array(':team_id' => $checked, ':group_id' => $group_id));
}
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
$row1[] = $row;
}

}

最佳答案

foreach循环中使用while循环,因为它会导致id问题

$group_id = $_POST['group_id'];
$team_id = $_POST['team_id'];
if(!empty($_POST['team_id'])) {
foreach($_POST['team_id'] as $checked) {
$sql = $db->prepare("SELECT a.team, a.player, a.result, a.note, b.category FROM teams a INNER JOIN groups b ON a.team_id = b.id WHERE a.team_id = :team_id_id AND a.group_id = :group_id ");
$sql->execute(array(':team_id' => $checked, ':group_id' => $group_id));
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
$row1[] = $row;
}
}
}

关于php - foreach 循环中的复选框值获取不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52126764/

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