0

I am inserting answer of each of my questions I need to get the question id and inserted into the answer table

see my code

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

What I am getting now is the last question id for all the answers I need to insert the id of the question with it's value.

edit

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

still can't get the right qid

can any one help please

1
Contributor
1
Reply
31
Views
2 Years
Discussion Span
Last Post by johnef_sh
0

I am getting this on print_r($_POST);

Array
(
    [qid] => 26
    [answer] => Array
        (
            [0] => this is anset
            [1] => saldjhaskl
            [2] => lkjklj
            [3] => lkjlkjlkj
            [4] => lkjlkjbh
            [5] => ,mbn
            [6] => ,mb,
            [7] => mnk
            [8] => hkl
            [9] => lkjhlkjl
        )

    [ans] => I'm Finished
)
1

Array
(
    [qid] => 26
    [answer] => Array
        (
            [0] => this is anset
            [1] => saldjhaskl
            [2] => lkjklj
            [3] => lkjlkjlkj
            [4] => lkjlkjbh
            [5] => ,mbn
            [6] => ,mb,
            [7] => mnk
            [8] => hkl
            [9] => lkjhlkjl
        )

    [ans] => I'm Finished
)
1

Array
(
    [qid] => 26
    [answer] => Array
        (
            [0] => this is anset
            [1] => saldjhaskl
            [2] => lkjklj
            [3] => lkjlkjlkj
            [4] => lkjlkjbh
            [5] => ,mbn
            [6] => ,mb,
            [7] => mnk
            [8] => hkl
            [9] => lkjhlkjl
        )

    [ans] => I'm Finished
)
1

Array
(
    [qid] => 26
    [answer] => Array
        (
            [0] => this is anset
            [1] => saldjhaskl
            [2] => lkjklj
            [3] => lkjlkjlkj
            [4] => lkjlkjbh
            [5] => ,mbn
            [6] => ,mb,
            [7] => mnk
            [8] => hkl
            [9] => lkjhlkjl
        )

    [ans] => I'm Finished
)
1
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.