如何使用PHP检索通过表单提交的数据

时间:2017-04-10 06:52:39

标签: php html mysql sql post

早上偷看,

我有一个PHP函数,它从我的数据库中获取10个问题,循环这些并在表单中填充它。对于每个问题,有4种答案可供选择(多种选​​择方式)。

对于每个答案记录,都有一栏说明它是否正确。所以,如果我能得到答案,我将能够检查它是否正确。

这就是我的HTML外观:

<div class="quiz">
    <?php
        echo '
            <form id="level_quiz" name="upload_form" action="calculate_result" method="post" enctype="multipart/form-data">';

            foreach (get_questions($_POST['level_id']) as $questions) {
                echo '
                        <div style="background-color: #242424 ! important; margin-top: 30px;" class="container-fluid bg-info">
                            <div class="modal-dialog">
                                <input type="hidden" id="question[]" name="question[]" value="'.$questions['question'].'">
                                <h2 name="question" id="question" value="'.$questions['question'].'" style="font-size: 25px;margin-bottom: 10px;margin-top: -30px;color: white;">'.$questions['question'].'</h2>
                                <div class="modal-content">
                                </div>
                            </div>
                        </div>';

                foreach (get_answers($questions['questions_id']) as $answers) {
                    echo '
                        <div class="question_style form-check form-check-inline">
                            <label class="form-check-label">
                                <input style="color: #ffffff" class="form-check-input" type="radio" name="answer[]" id="answer[]" value=" '. $answers['answer_id'] .' "> '. $answers['answer'] .'
                            </label>
                        </div>
                ';
                }

            }
        echo '<button style="float: right;" type="submit" class="btn btn-primary">Submit</button></form>';
    ?>
</div>

PHP函数: get_questions()

function get_questions($id) {
    $sql = <<<SQL
    SELECT course.name, course.level_type, quiz.name, quiz.total_questions, questions.question
    FROM `course`
    JOIN quiz using (course_id)
    JOIN questions using (quiz_id)
    JOIN answers using (questions_id)
    WHERE course.course_id = '$id'
    AND answers.correct = 1
    GROUP BY answers.questions_id
SQL;

    $result = get()->query($sql);

    $rows = resultToArray($result);
    return $rows;
}

此功能将返回我所有问题属于答案正确的级别,以便我可以获得问题列表。

get_answers()

function get_questions_and_all_possible_answers($quetion_id) {
    $sql = <<<SQL
    SELECT *
    FROM `answers`
    WHERE `questions_id` = '$quetion_id'
SQL;

    $result = get()->query($sql);

    $rows = resultToArray($result);
    return $rows;
}

当我传入一个问题ID时,上面的这个函数得到了我所有可能的答案(如上所述,一个问题有4个答案,用户可以选择正确的答案)。

我的问题是,当我提交表单时,我会收到所有10个问题,但是我无法看到用户为每个问题记下了什么。是否有一些我可以更改代码的方式,以便我可以获得用户为每个问题选择的内容,这样我就可以在我发布表单的任何地方进行检查。

目前表单提交的方式如下:

Array ( [question] => Array ( [0] => What is forex trading ? [1] => Currency pairs are usually represented by how many letters? [2] => The first two letters in a currency pair stand for? [3] => What does this stand for in trading terms NZD? [4] => What is the largest traded market? [5] => What does a liquid market mean for a trader? [6] => What is the largest traded currency? [7] => How are forex quotes arranged? [8] => Selling a currency to buy another is referred to in forex terms as? [9] => Which currency is the basis for the buy or the sell? ) [answer] => Array ( [0] => ) )

我已经尽力解决这个问题但是我无法在这里发布,如果需要更多细节请询问。

提前谢谢你。

问题表的快照:

https://gyazo.com/139d78ae9f71dfdda7a4e8c89d33a41b

答案表的快照:

https://gyazo.com/7fbd116f3bb9dd1d28c6e450d8c087c5

课程表的快照(每门课程都会有问题)

https://gyazo.com/61ab61c5a8a240cacf3ace72c10c656b

0 个答案:

没有答案