SamDasti
SamDasti

Reputation: 87

checkbox value fetch in foreach loop not working

With the below given code i am trying to fetch data of check boxes from MySQL database, Which is not working and fetches only last checked value.

With var_dump($checked); its giving correct result.

PHP Script

$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;
        }

}

Upvotes: 0

Views: 95

Answers (1)

Mooni
Mooni

Reputation: 143

Use while loop inside foreach loop because its causing id problem

$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;
    }
}
}

Upvotes: 1

Related Questions