I want to output the question number (vraag) with a for loop so it gets 5 elements from the database (from 1 to 5). Then I want to get the answer from the user input (A/B/C/D)
Get the answer values from the database based on the question number with the user input value.
The problem is, it only outputs the first element and the rest of the output is empty.
Included is a picture of the database information.Database
for($i = 1; $i < 6; $i++){
$vraag = $i;
$answer = '$answer'.$i;
$answerNumber = $_POST['question-'.$i.'-answers'];
$query = $db->prepare("SELECT * FROM vraag WHERE vraagnummer = :VRAAG AND antwoord = :ANTWOORD");
$query->bindparam(":VRAAG",$vraag);
$query->bindparam(":ANTWOORD",$answerNumber);
$query->execute();
$has_author = $query->fetch();
$VN = $has_author['vraagnummer'];
$A = $has_author['antwoord'];
$PA = $has_author['puntenA'];
$PB = $has_author['puntenB'];
$PC = $has_author['puntenC'];
$PD = $has_author['puntenD'];
echo $VN;
echo $A;
echo '<br />';
echo '<br />';
echo 'A'.$PA.'<br />';
echo 'B'.$PB.'<br />';
echo 'C'.$PC.'<br />';
echo 'D'.$PD.'<br />';
}
To display whole result of database you need to use a while loop
as
while ($has_author = $query->fetch()) {
$VN = $has_author['vraagnummer'];
$A = $has_author['antwoord'];
$PA = $has_author['puntenA'];
$PB = $has_author['puntenB'];
$PC = $has_author['puntenC'];
$PD = $has_author['puntenD'];
echo $VN;
echo $A;
echo '<br />';
echo '<br />';
echo 'A' . $PA . '<br />';
echo 'B' . $PB . '<br />';
echo 'C' . $PC . '<br />';
echo 'D' . $PD . '<br />';
}