How to display database table rows into an html form with for or while loop?

There are two columns: id & questions, which have five rows. I want to display each row in the input value, with the name attribute as ‘quest’. The input with name attribute ‘ans’ will remain blank for answer. The problem is the loop displays each row five times, printing either each question five times, or only one question five times. I tried declaring a variable $i = 1 and incremented it as $i++ after every second input, but I am unable to fix the problem. Could anyone please help me out with your insight?

<form action="code.php" method="post" class="form-inline">

<?php

    $query = "SELECT questions FROM stpaper";

    $result = mysqli_query($conn, $query);

    $row_count = mysqli_num_rows($result);

    while ($row = mysqli_fetch_array($result)) {

?>

    <label class="label col-md-3 control-label">Q.1</label>

    <input type="text" class="form-control" name="quest" value="<?php echo $row['questions']; ?>" >

    <label class="label col-md-3 control-label">Ans.</label>

    <input type="text" class="form-control" name="ans" value="">

    <label class="label col-md-3 control-label">Q.2</label>

    <input type="text" class="form-control" name="quest" value="<?php echo $row['questions']; ?>" >

    <label class="label col-md-3 control-label">Ans.</label>

    <input type="text" class="form-control" name="ans" value="">

    <label class="label col-md-3 control-label">Q.3</label>

    <input type="text" class="form-control" name="quest" value="<?php echo $row['questions']; ?>" >

    <label class="label col-md-3 control-label">Ans.</label>

    <input type="text" class="form-control" name="ans" value="">

    <label class="label col-md-3 control-label">Q.4</label>

    <input type="text" class="form-control" name="quest" value="<?php echo $row['questions']; ?>" >

    <label class="label col-md-3 control-label">Ans.</label>

    <input type="text" class="form-control" name="ans" value="">

    <label class="label col-md-3 control-label">Q.5</label>

    <input type="text" class="form-control" name="quest" value="<?php echo $row['questions']; ?>" >

    <label class="label col-md-3 control-label">Ans.</label>

    <input type="text" class="form-control" name="ans" value="">

<?php  

}

mysqli_close($conn);

?>

<button type="submit" class="btn btn-success mt-4 mx-5" name="submit" id="submit">Submit</button>

</form>

I don’t know what that means. For testing, eliminate the query and just make an array of data:

$rows = [
    ['id' => 1, 'question' => 'Question 1'],
    ['id' => 2, 'question' => 'Question 2'],
];
foreach($rows as $row) {

Or whatever your actual data format is.

Next you need to adjust your element naming to allow for multiple values. At least I think you want a form in which an user can answer multiple questions with one submit. Something like:

<input name="ans[1]" 
<input name="ans[2]"

And just as a side note, abbreviations really don’t help especially when maintaining code. Use answer instead of ans and your future self will thank you.

1 Like

Well, it will do. You loop through the results, and output $row['questions'] five times. You should just output it once - that’s the point of the loop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.