我无法将QuestionId
插入Answer
表格。
Question
插入没有问题,但我试图从QuestionId
表中检索Question
,这是Answer
表中的自动增量特定SessionId
所属的QuestionNo
和QuestionId
。相反,当插入0
表时,它会QuestionId
显示Answer
。
现在我知道尝试从QuestionId
表中为每个Question
执行选择的查询是正确的,因为已经过测试。我认为问题出在我放置代码但我不确定的地方?
以下是db表:
问题表
QuestionId (auto) SessionId QuestionNo
4 2 1
5 2 2
6 2 3
答案表:
AnswerId (auto) QuestionId Answer
7 0 A
8 0 C
9 0 A
10 0 B
11 0 True
答案表应该是什么样的:
AnswerId (auto) QuestionId Answer
7 4 A
8 4 C
9 5 A
10 5 B
11 6 True
以下是代码:
$questionsql = "INSERT INTO Question (SessionId, QuestionNo)
VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
$answersql = "INSERT INTO Answer (QuestionId, Answer)
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( $insert && $insertanswer)
{
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
$c = count($_POST['numQuestion']);
for($i = 0; $i < $c; $i++ )
{
$insert->bind_param("ii", $sessionid, $_POST['numQuestion'][$i]);
$insert->execute();
if ($insert->errno)
{
// Handle query error here
echo __LINE__.': '.$insert->error;
break 1;
}
}
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$lastID = $id;
$questionidquery = "SELECT QuestionId FROM Question WHERE (QuestionNo = ? AND SessionId = ?)";
if (!$questionidstmt = $mysqli->prepare($questionidquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
// Bind parameter for statement
$questionidstmt->bind_param("ii", $lastID, $sessionId);
// Execute the statement
$questionidstmt->execute();
if ($questionidstmt->errno)
{
// Handle query error here
echo __LINE__.': '.$questionidstmt->error;
break 2;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$questionidstmt->bind_result($quesid);
// This populates $optionid
$questionidstmt->fetch();
$questionidstmt->close();
foreach($value as $answer)
{
$insertanswer->bind_param("is", $quesid, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 3;
}
}
}
//close your statements at the end
$insertanswer->close();
$insert->close();
}
?>
更新
var_dump($ _ POST);
的结果array(8) {
["numberAnswer"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "1"
}
["numQuestion"]=> array(2)
{
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["questionText"]=> array(2) {
[0]=> string(12) "What is 2+2?"
[1]=> string(12) "What is 4+4?"
}
["gridValues"]=> array(2) {
[0]=> string(1) "4"
[1]=> string(1) "4"
}
["reply"]=> array(2) {
[0]=> string(6) "single"
[1]=> string(6) "single"
}
["textWeight"]=> array(2) {
[0]=> string(1) "5"
[1]=> string(1) "5"
}
["submitDetails"]=> string(14) "Submit Details" ["value"]=> array(2) {
[1]=> array(1) {
[0]=> string(1) "B"
}
[2]=> array(1) {
[0]=> string(1) "D"
}
}
}
答案 0 :(得分:1)
现在你的QuestionID来自$_POST['value']
,这可能不是你想要的......你需要在插入每个问题后从mysql中获取自动生成的QuestionID。
您可以使用$mysqli->insert_id
检索上次生成的ID。每次调用$insert->execute();
后执行此操作,然后在插入答案时使用这些ID。
您应该使用$mysqli->insert_id
,因为insert_id
仅对连接对象有效(您的名称为$mysqli
)。
您还需要将insert_id分配给某个东西。我在跟踪你的代码时遇到了麻烦,但我认为你想要的是将问题ID存储在一个数组中供以后使用,如下所示:
$question_ids[$i] = $mysqli->insert_id
在此之后我想要做的事情让我感到茫然,我想你会用一个相应的问题编号来解答一些答案,但是...也许你可以发布var_dump($_POST)
的输出我们会更好理解。在任何情况下,您都需要某种循环来插入每个问题的答案,并且您可以(希望)使用数组$question_ids[$i]
来检索QuestionID。
以下是一些示例代码(未经测试),尝试在使用$_POST
和$_SESSION
<?php
$questions = array(
array(2, 1),
array(2, 2),
# etc
);
$answers = array(
array(A, C),
array(A, B),
);
$questionsql = "INSERT INTO Question (SessionId, QuestionNo) VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
die('couldn\'t prepare statement 1');
}
$answersql = "INSERT INTO Answer (QuestionId, Answer) VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
die('couldn\'t prepare statement 2');
}
$c = count($questions);
for($i = 0; $i < $c; $i++)
{
$insert->bind_param("ii", $questions[$i][0], $questions[$i][1]);
$insert->execute();
if ($insert->errno)
{
die("Error inserting question $i");
}
$lastID = $mysqli->insert_id;
foreach ($ans in $answers[$i])
{
$insertanswer->bind_param("is", $lastID, $ans);
$insertanswer->execute();
if ($insertanswer->errno) {
die("Error inserting answer to question $i");
}
}
}
$insertanswer->close();
$insert->close();
?>