질문 :외래 키가 생성되지 않는
가 (이 쿼리를해야합니다) 아래 선택 쿼리를 수행 할 :
SELECT QuestionId 질문 FROM WHERE (QuestionNo = 및 세션 ID = ?)
Question 테이블에서 QuestionId를 찾을 수 있으려면 이 답변 테이블에 저장되어 모든 답변에 대해 이 답변을 결정할 수 있습니다. ERS는 질문에 속해
문제 :
mysqli 코드의 문제가 올바른 QuestionId 값을 삽입 할 수없는 것입니다. Answer
테이블에 QuestionId가 0으로 표시됩니다. 올바른 QuestionId를 표시 할 수 있도록 누군가가이를 수정할 수 있습니까?
맨 위에 제공된 SELECT 쿼리를 수행해야합니다. 나는 그것을 mysqli에서 사용해야한다. 순간
질문 표
QuestionId (auto) SessionId QuestionNo
4 2 1
5 2 2
6 2 3
응답 테이블 : : 아래
AnswerId (auto) QuestionId Answer
7 4 A
8 4 C
9 5 A
10 5 B
11 6 True
:
AnswerId (auto) QuestionId Answer
7 0 A
8 0 C
9 0 A
10 0 B
11 0 True
어떤 대답 표과 같아야 여기
는 DB의 테이블입니다 는 코드 :
직접 삽입 한 후 질문에 autoincrementing ID로 사용되는 시퀀스의 마지막 값을 검색 할 필요가$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();
}
?>
어떤 데이터베이스를 사용하고 있습니까? 어떤 오류가 있습니까? – Oded
"작성되지 않았습니다"라는 의미는 무엇입니까? –
올바른 데이터베이스에 외래 키를 만들고 테스트하지 않았습니까? 또는 개발 데이터베이스? –