循环我的测验ID并获取每个并插入答案表

时间:2014-11-08 12:58:47

标签: php mysql

每次$qid插入数据库1

时,我都需要获取问题ID并将其插入答案表

这是我的db表看起来像

┌──┬───┬─────┬───┬────────┐
│id│uid│ad_id│qid│answer  │
├──┼───┼─────┼───┼────────┤
│1 │2  │15   │1  │Answer 1│
├──┼───┼─────┼───┼────────┤
│2 │2  │15   │1  │Answer 2│
├──┼───┼─────┼───┼────────┤
│3 │2  │15   │1  │Answer 3│
├──┼───┼─────┼───┼────────┤
│4 │2  │15   │1  │Answer 4│
└──┴───┴─────┴───┴────────┘

现在,列qid应该是来自问题表的问题ID,但它并不像你看到的那样,总是插入1

这是我的PHP代码

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" id="quiz" class="container width_648">

<?php
            $getTheQuiz = $db->prepare("SELECT * FROM rec_employer_quiz WHERE ad_id=?");
            $getTheQuiz->bind_param('i', $ad);
            if ($getTheQuiz->execute()) {
                $res = $getTheQuiz->get_result();
                $i = 0;
                while ($q = $res->fetch_array()) {
                    ?>
                    <div class="oneLine">
                        <div class="question">
                            <h3><?php printf("%s", $q['question']) ?></h3>
                            <input type="hidden" value="<?php printf("%s", $q['id']) ?>" name="qid" id="qid">
                        </div>
                        <div class="answer">
                            <input type="hidden" name="qid" id="qid" value="<?php printf("%s", $q['id']) ?>">
                            <textarea name="answer[]" rows="3" maxlength="200" class="message1 width_640"></textarea>
                        </div>
                    </div>
                <?php
                }
                ?>
                <div class=" oneLine">
                    <input type="submit" name="ans" id="ans" value="I'm Finished" class=" MainBtn">
                </div>

                <?php
                $dateApplied = date(date_default_timezone_get());
                if (isset($_POST['ans'])) {
                    //$questionId=$_POST['qid'];
                    foreach ($_POST["answer"] as $key => $answer) {
                        $questionId=$_POST['qid'];
                        $answer = $_POST["answer"][$key];
                        $putData = $db->prepare("INSERT INTO rec_employer_quiz_results (id, uid, ad_id, qid, answer, exam_date)VALUE(?, ?, ?, ?, ?, FROM_UNIXTIME(?))");
                        $putData->bind_param('iiiiss', $id, $uid, $ad, $questionId, $answer, $dateApplied);
                        if ($putData->execute()) {
                            echo "done";
                        } else {
                            printf("Error: %s\n", $db->error);
                        }
                    }
                }
            }
            ?>
        </form>

我现在得到的是所有答案的最后一个问题id 我需要用它的值来插入问题的id。

修改

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" id="quiz" class="container width_648">


        <?php
        $getTheQuiz = $db->prepare("SELECT * FROM rec_employer_quiz WHERE ad_id=?");
        $getTheQuiz->bind_param('i', $ad);
        if ($getTheQuiz->execute()) {
            $res = $getTheQuiz->get_result();
            while ($q = $res->fetch_array()) {
                ?>
                <div class="oneLine">
                    <div class="question">
                        <input type="hidden" name="qid[<?php printf("%s", $q['id']) ?>]" value="qid[<?php printf("%s", $q['id']) ?>]">
                        <h3><?php printf("%s", $q['question'])?></h3>
                    </div>
                    <div class="answer">
                        <label for="answer"></label>
                        <textarea name="answer[]" id="answer" rows="3" maxlength="200" class="message1 width_640"></textarea>
                    </div>
                </div>
            <?php
            }
            ?>
            <div class=" oneLine">
                <input type="submit" name="ans" id="ans" value="I'm Finished" class=" MainBtn">
            </div>


            <?php
            $dateApplied = date(date_default_timezone_get());
            if (isset($_POST['ans'])) {
                $questionId = $_POST['qid'];
                foreach ($_POST["answer"] as $key => $answer) {
                    $answer = $_POST["answer"][$key];
                    $putData = $db->prepare("INSERT INTO rec_employer_quiz_results (id, uid, ad_id, qid, answer, exam_date)VALUE(?, ?, ?, ?, ?, FROM_UNIXTIME(?))");
                    $putData->bind_param('iiiiss', $id, $uid, $ad, $questionId, $answer, $dateApplied);
                    if ($putData->execute()) {
                        print_r($_POST);
                    } else {
                        printf("Error: %s\n", $db->error);
                    }
                }
            }
        }
        ?>
    </form>

我在print_r($_POST);

上收到此消息
Array
(
    [qid] => Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
            [3] => 18
            [4] => 19
            [5] => 20
            [6] => 21
            [7] => 22
            [8] => 23
            [9] => 26
        )

    [answer] => Array
        (
            [0] => Answer 
            [1] => Answer 2
            [2] => Answer 3
            [3] => Answer 5
            [4] => Answer 6
            [5] => Answer 7
            [6] => Answer 8
            [7] => Answer 9
            [8] => Answer 10
            [9] => Answer 11
        )

    [ans] => I'm Finished
)
1

但是在我的数据库中检查插入的qid时,只需为所有行插入1

1 个答案:

答案 0 :(得分:1)

让我们在这里考虑您的代码:

<div class="question">
    <input type="hidden" value="<?php printf("%s", $q['id']) ?>" name="qid" id="qid">
 </div>
 <div class="answer">
    <input type="hidden" name="qid" id="qid" value="<?php printf("%s", $q['id']) ?>">
    <textarea name="answer[]" rows="3" maxlength="200" class="message1 width_640"></textarea>
</div>

此处您已有2个名称为qid的字段。在您post表单中的所有字段后,将保存最后一个qid值。另外,我不明白为什么每个问题需要2 qid个字段?一个领域还不够?

您需要做的是将qid设置为answer,即

<input type="hidden" name="qid[]" id="qid" value="<?php printf("%s", $q['id']) ?>">

Print_r$_POST的价值观,然后看看它们是如何安排的。

更进一步,您可以使用当前$q['id']作为字段名称的一部分:

<input type="hidden" name="qid[<?php printf("%s", $q['id']) ?>]" id="qid" value="<?php printf("%s", $q['id']) ?>">

也可以回答:

<textarea name="answer[<?php printf("%s", $q['id']) ?>]" rows="3" maxlength="200" class="message1 width_640"></textarea>

再次print_r$_POST的价值观,然后看看它们是如何排列的。

旁注:字段的id属性对于每个字段应该是唯一的。

<强>更新: 所以,我看到你得到适当的qids。您现在应该使用qidanswer中的相同密钥:

foreach ($_POST["answer"] as $key => $answer) {
    $answer = $_POST["answer"][$key];
    $questionId = $_POST['qid'][$key];
    // do insert
}