我正在努力插入数据并能够从数据库中选择一个 AnswerId。这就是我想要做的:
以下是我目前拥有的表格作为示例:
我从下面的查询中获取上述详细信息的详细信息(“每个答案的分数”列除外):
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.Answer, q.QuestionMarks
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
";
查询结果:
上面查询结果中的每个答案实际上在“答案”表中都有自己的 AnswerId,结果如下:
下面是显示上表是如何制作的视图源代码:
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<tr class="questiontd">
<td class="questionnumtd q1_qnum" name="numQuestion" rowspan="3">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="4"><input type="hidden" name="q1_ans" class="q1_ans" value="4"></td>
<td class="questioncontenttd" rowspan="3">Here are 3 answers </td>
<td class="answertd" name="answers[]">B</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="totalmarkstd" rowspan="3">4</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="3"><strong>4</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">F</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<tr class="questiontd">
<td class="questionnumtd q2_qnum" name="numQuestion" rowspan="2">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="6"><input type="hidden" name="q2_ans" class="q2_ans" value="6"></td>
<td class="questioncontenttd" rowspan="2">What 2 speeds can you do in a carriageway (single and dual) </td>
<td class="answertd" name="answers[]">A</td>
<td class="answermarkstd">
<input class="individualMarks q2_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="2" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="totalmarkstd" rowspan="2">6</td>
<td class="noofmarkstd q2_ans_text" q_group="1" rowspan="2"><strong>6</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">C</td>
<td class="answermarkstd">
<input class="individualMarks q2_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="2" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
</tbody>
</table>
<p>
<input type='hidden' id='num_groups' name='num_groups' value='2'>
<input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" />
</p>
</form>
我遇到的问题是我的插入。我想将数据插入到“Individual_Answer”表中的“AnswerId”和“AnswerMarks”表中,这样我们就知道每个答案值多少分。因此,在提交顶层表并插入后,数据库表应如下所示:
Individual_Answer 表:
AnswerId AnswerMarks
295 2
296 1
297 1
298 3
299 3
我似乎无法弄清楚如何首先为每个答案检索“AnswerId”,然后才能将数据插入数据库。下面是我的尝试,但我的问题是有人可以修复下面的代码以便能够检索“AnswerId”然后正确插入数据吗?
下面是我的尝试(我使用 mysqli/php):
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
var_dump($_POST);
$answersql = "INSERT INTO Individual_Answer (AnswerId, AnswerMarks)
VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
//make sure both prepared statements succeeded before proceeding
if($insertanswer)
{
$sessid = $_SESSION['id'] . ($_SESSION['initial_session'] > 1 ? $_SESSION['sessionCounting'] : '');
$c = count($_POST['numQuestion']);
for($i = 0; $i < $c; $i++ )
{
$answerquery = "SELECT AnswerId FROM Answer WHERE (SessionId = ? AND QuestionId = ? and Answer = ?)";
if (!$answerstmt = $mysqli->prepare($answerquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
// Bind parameter for statement
$answerstmt->bind_param("iis", $sessid, $_POST['numQuestion'][$i], $_POST['answers'][$i]);
// Execute the statement
$answerstmt->execute();
if ($answerstmt->errno)
{
// Handle query error here
echo __LINE__.': '.$answerstmt->error;
break 1;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$answerstmt->bind_result($dbAnswerId);
// This populates $optionid
$answerstmt->fetch();
$answersql->bind_param('ii', $dbAnswerId, $_POST['answers'][$i]);
$answerstmt->close();
}
//close your statements at the end
$insertanswer->close();
}
?>
var_dump($_POST) 在下面显示:
array(7) {
["q1_ans_org"]=> string(1) "4"
["q1_ans"]=> string(1) "0"
["answerMarks"]=> array(5)
{
[0]=> string(1) "2"
[1]=> string(1) "1"
[2]=> string(1) "1"
[3]=> string(1) "3"
[4]=> string(1) "3" }
["q2_ans_org"]=> string(1) "6"
["q2_ans"]=> string(1) "0"
["num_groups"]=> string(1) "2"
["submitMarks"]=> string(12) "Submit Marks"
}
Notice: Undefined index: numQuestion in ... on line 55