0

In a quiz app, I am generating Radio options for each question dynamically from database. I need to store the chosen answer of the questions the user submitted in a database table. I want to know which user picked which answer for which question.

Here is my form:

    <form id="question" class="" action="quiz_ans.php" method="post">
        <table id="quiz-question" align="center" class="row-border compact order-column stripe">
            <input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
            <thead>
                <?php
                    if($QuizQuestions) {
                        $i=1;
                        foreach($QuizQuestions as $row):
                  ?>
                <tr>
                    <th><?php echo $i; ?>. <?php echo $row->Question; ?>
                        <br>
                    <?php if(isset($row->Screenshot)) { ?>
                        <img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
                    <?php } ?>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php if(isset($row->Option1)) { ?>
                <tr class="info">
                    <td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
                </tr>
                <?php } ?>
                <?php if(isset($row->Option2)) { ?>
                <tr class="info">
                    <td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
                </tr>
                <?php } ?>
                <?php if(isset($row->Option3)) { ?>
                <tr>
                    <td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
                </tr>
                <?php } ?>
                <?php if(isset($row->Option4)) { ?>
                <tr>
                    <td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
                </tr>
                <?php } ?>
                <tr>
                    <td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
                </tr>
                <?php if(isset($row->Id)) { ?>
                <tr>
                    <td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
                </tr>
                <?php } ?>


            </tbody>

        <?php
            $i++;
          endforeach;
          }
        ?>
        </table>
        <br>
        <input type="submit" name="submit" value="Submit" class="btn btn-success">
    </form>

This is how I tried to store the data in database table:

    $NumberofQuestions = $_POST['NumberofQuestions'];
for ($i=0; $i<$NumberofQuestions; $i++)
{
    $sql = "INSERT INTO tblquizresponse (QuestionId, AnswerId, AnswerReason, UserId, SubmittedAt) VALUES (' ".$_POST['QuestionId'] [$i]." ',
        ' ".$_POST['AnswerId'] [$i]." ', ' ".$_POST['AnswerReason'] [$i]." ', ' ".$UserId." ', ' ".$SubmittedAt." ')";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
}

But I do not get the AnswerId stored in database. Here is the array I get if the form is submitted:

Array
(
[NumberofQuestions] => 5
[AnswerId] => Array
    (
        [16] => 0
        [17] => 1
        [18] => 2
        [74] => 3
        [75] => 0
    )

[AnswerReason] => Array
    (
        [0] => Test answer one reason.
        [1] => Test answer two reason.
        [2] => Test answer three reason.
        [3] => Test answer four reason.
        [4] => Test answer five reason.
    )

[QuestionId] => Array
    (
        [0] => 16
        [1] => 17
        [2] => 18
        [3] => 74
        [4] => 75
    )

[submit] => Submit
)

Here is the data stored in table after submit:

Id    QuestionId    AnswerId    AnswerReason               UserId      SubmittedAt
1    16             0           Test answer one reason.    xxxxxxxx    2020-02-26 12:38:53
2    17             0           Test answer two reason.    xxxxxxxx    2020-02-26 12:38:53
3    18             0           Test answer three reason.  xxxxxxxx    2020-02-26 12:38:53
4    74             0           Test answer four reason.   xxxxxxxx    2020-02-26 12:38:53
5    75             0           Test answer five reason.   xxxxxxxx    2020-02-26 12:38:53

How do I get the AnswerId stored in the database as well? Any help would be much appreciated.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Lone Rider
  • 97
  • 2
  • 10
  • 1
    The `AnswerId` array looks to be indexed by `QuestionId`. You may want to reference those values accordingly, with something like `$_POST['AnswerId'][$_POST['QuestionId'][$i]]`. Also, I strongly recommend using [prepared statements](https://stackoverflow.com/a/24989031/924299), as your code is vulnerable to SQL injection. – showdev Feb 26 '20 at 07:23
  • This question has nothing to do with PDO database API, it's actually a question how to get an HTML from data in PHP. It is not "PDO" who doesn't store your answer. You are just feeding a non-existent value to it. As soon as you'll manage to get the value, it will be inserted all right. However, as PDO is mentioned in your question, be aware you are using a [cargo cult prepared statement](https://phpdelusions.net/pdo/cargo_cult_prepared_statement) that never does anything useful. – Your Common Sense Feb 26 '20 at 07:27

0 Answers0