0

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

}
10
  • 1
    Can you add the part of the code where you define $row1 and where you read it out?. And I guess you'll want to move the while loop inside the foreach. Now the $sql variable might not be defined inside your while statement Commented Sep 1, 2018 at 9:27
  • @jrswgtr I am getting data via ajax, It will be a bit difficult for me to share a bunch of code. Commented Sep 1, 2018 at 9:31
  • Then I doubt if we will be able to help you out Commented Sep 1, 2018 at 9:32
  • @jrswgtr ok ! i am trying to do it. Commented Sep 1, 2018 at 9:34
  • 1
    because with in your foreach sends last value. did you try while loop inside foreach? Commented Sep 1, 2018 at 10:07

1 Answer 1

1

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;
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.