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

时间:2018-09-01 09:25:13

标签: php mysql checkbox foreach

使用下面给出的代码,我试图从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;
        }

}

1 个答案:

答案 0 :(得分:0)

foreach loop内部使用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;
    }
}
}