gpt4 book ai didi

php - 它没有在答案数据库表中插入questionid

转载 作者:行者123 更新时间:2023-11-29 14:06:59 25 4
gpt4 key购买 nike

我无法将 QuestionId 插入到 Answer 表中。

Question 插入没有问题,但我试图从 Question 表中检索 QuestionId,该表是自动增量到通过查找特定 QuestionId 所属的 SessionIdQuestionNo 来生成 Answer 表。相反,当插入 Answer 表时,它会一直显示 QuestionId0

现在我知道尝试从 Question 表中为每个 QuestionId 执行选择的查询是正确的,因为这已经过测试。我认为问题出在我放置代码的位置但我不确定?

这是数据库表:

问题表

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

最佳答案

现在您的 QuestionID 来自 $_POST['value'],这可能不是您想要的...您需要在插入每个问题后从 mysql 获取自动生成的 QuestionID .

您可以使用$mysqli->insert_id检索最后生成的ID。每次调用 $insert->execute(); 后执行此操作,然后在插入答案时使用这些 ID。

编辑1:

您应该使用$mysqli->insert_id,因为insert_id仅在连接对象上有效(您的连接对象称为$mysqli) 。

您还需要将 insert_id 分配给某些内容。我在遵循您的代码时遇到问题,但我认为您想要的是将问题 ID 存储在数组中以供以后使用,如下所示:

$question_ids[$i] = $mysqli->insert_id

我对你在这之后想要做什么感到茫然,我猜你会用相应的问题编号提取一些答案,但是......也许如果你可以发布 var_dump($ _POST)我们会更好地理解。无论如何,您都需要某种循环来插入每个问题的答案,并且您可以(希望)使用数组 $question_ids[$i] 来检索 QuestionID。

编辑2:

这里是一些示例代码(未经测试),在使用 $_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();
?>

关于php - 它没有在答案数据库表中插入questionid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com