Сохраните массив значений параметров Dynami c в MySQL, используя PDO - PullRequest
0 голосов
/ 26 февраля 2020

В приложении для викторины я динамически генерирую параметры радио для каждого вопроса из базы данных. Мне нужно сохранить выбранный ответ на вопросы пользователя, представленные в таблице базы данных. Я хочу знать, какой пользователь выбрал, какой ответ на какой вопрос.

Вот моя форма:

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

Вот как я пытался сохранить данные в таблице базы данных:

    $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();
}

Но я не получаю answerId, хранящийся в базе данных. Вот массив, который я получаю, если форма отправлена:

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
)

Вот данные, хранящиеся в таблице после отправки:

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

Как мне получить AnswerId, хранящийся в базе данных также? Любая помощь будет высоко ценится.

...