드롭 다운 메뉴에서 개별 학생을 선택하면 이상한 ubt입니다 (의 값인 $_POST['student'] != 0
은 All
옵션을 나타내고 다른 모든 옵션은 자신의 번호를 가짐). 오류 진술 :정의되지 않은 변수 및 잘못된 인수가 제공됩니다.
Notice: Undefined variable: questions in ... on line 597 Warning: Invalid argument supplied for foreach() in ... on line 597
내 질문은 무엇을이 오류의 원인이되고 어떻게 고칠 수 있나요?
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname
...
FROM Student st
...
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
//check if POST is empty
// Check whether a specific student was selected
$p_student = empty($_POST["student"])?0:$_POST["student"]; // Now if $_POST['student'] is either 0 or empty $p_student will be 0
switch($p_student){
case 0:
//dont' add where filters
break;
default:
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
$p_question = empty($_POST["question"])?0:$_POST["question"]; // Same here, if $_POST['question'] is either 0 or empty $p_question will be 0
switch($p_question){
case 0:
//dont' add where filters
break;
default:
$where[] = 'q.QuestionId = ?';
$parameters[] .= $_POST["question"];
$parameterTypes .= 'i';
}
// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
// You only need to call bind_param once
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
if (count($where) == 1) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) == 2) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) == 3) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
}
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
$arrStudentId = array();
$arrStudentAlias = array();
...
while ($selectedstudentanswerstmt->fetch()) {
//Check if the student data exist.
if (!isset($questions[$detailsStudentId])) {
$questions[$detailsStudentId] = array(
'studentalias' => $detailsStudentAlias,
'studentforename' => $detailsStudentForename,
'studentsurname' => $detailsStudentSurname,
'questions' => array()
);
}
$questions[$detailsStudentId]['questions'][$detailsQuestionId] = array(
'questionno'=>$detailsQuestionNo,
'content'=>$detailsQuestionContent,
.....
);
}
$selectedstudentanswerstmt->close();
?>
//LINE 597 ERROR BELOW
foreach ($questions as $studentId => $studentData) {
echo '<hr><h3>'.$studentData['studentalias'].' - '.$studentData['studentforename'].' '.$studentData['studentsurname'].'</h3>';
foreach ($studentData['questions'] as $questionId => $questionData) {
echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>';
}
}
방금 소스의 일부를 게시 했으므로 정의가 if 문 또는 실행되지 않은 루프에있을 수 있습니다. 그 문제가 꽤 많이있는 – Benny
@ 베니를 찾으십시오. 나는 브래킷이나 콜론을 찾고 있다고 생각합니까? 아니면 정확히 무엇을 찾아야할까요? 내 코드 스 니펫의 '...'은 긴 코드 목록과 같은 것일 뿐이며, 대괄호 등을 포함하지 않으며, 내가 의미하는 바를 알면 코드를 더 작게 만듭니다. 여러분이 보는 구조는 풀 코드 – user2048994
과 완전히 같은 구조입니다. 예를 들어,'$ questions'의 모든 항목을 찾으십시오. 또는 중요한 부분과 적절하게 건너 뛰었 기 때문에 적어도 여기에 넣으십시오. 또는 while ($ selectedstudentanswerstmt-> fetch())'가 적어도 한 번 반복되었는지 확인하십시오. 그렇지 않으면 질문이 null이 될 수 있습니다. – Kyborek