I'm trying to fetch all of the position_id's in order to fill an array which I can then pass to the second statement to fetch all of the rows containing a position_id in the array. I'm trying to use this data to fill a dropdown menu.
Am I doing this properly or is there a better way to do this?
// $_POST['aid'] == election_id
if(isset($_POST['aid'])) {
// Get's the rows with corresponding election_id
$stm = $db->prepare("SELECT * FROM position_elections WHERE election_id = " . $_POST['aid']);
$stm->execute();
$position_ids = [];
if ($row = $stm->fetchAll(PDO::FETCH_ASSOC))
{
$position_id = $row['position_id'];
array_push($position_ids, $position_id);
}
$ids = join("','",$position_ids);
$stm2 = $db->prepare("SELECT * FROM positions WHERE position_id IN ($ids)");
$stm2->execute();
$positions = $stm2->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($positions);
}