Reputation: 101
I need to get the question id and insert it into the answer table every time the $qid
insert into the database 1
this is my db table look like
┌──┬───┬─────┬───┬────────┐
│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│
└──┴───┴─────┴───┴────────┘
now the column qid
should be the question id coming from the question table but it dose not as you see it's always insert 1
this is my PHP 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>
I am getting this on 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
but when check the inserted qid
in my database it just insert 1
for all the rows.
Upvotes: 0
Views: 405
Reputation: 54796
Let's consider your code here:
<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>
Here you already have 2 fields with name qid
. After you post
all fields from your form the last qid
value will be saved. Also I don't understand why do you need 2 qid
fields for each question? One field is not enough?
What you need to do is to set qid
as answer
, i.e
<input type="hidden" name="qid[]" id="qid" value="<?php printf("%s", $q['id']) ?>">
Print_r
you $_POST
values then and see how are they arranged.
Going further you can use current $q['id']
as part of field name:
<input type="hidden" name="qid[<?php printf("%s", $q['id']) ?>]" id="qid" value="<?php printf("%s", $q['id']) ?>">
And for answer too:
<textarea name="answer[<?php printf("%s", $q['id']) ?>]" rows="3" maxlength="200" class="message1 width_640"></textarea>
Again print_r
you $_POST
values then and see how are they arranged.
Sidenote: id
attribute for fields should be unique for each field.
Update:
so, I see you get proper qids. You should now use the same keys from qid
and answer
:
foreach ($_POST["answer"] as $key => $answer) {
$answer = $_POST["answer"][$key];
$questionId = $_POST['qid'][$key];
// do insert
}
Upvotes: 1