2013-02-19 3 views
0

드롭 다운 메뉴에서 개별 학생을 선택하면 이상한 ubt입니다 (의 값인 $_POST['student'] != 0All 옵션을 나타내고 다른 모든 옵션은 자신의 번호를 가짐). 오류 진술 :정의되지 않은 변수 및 잘못된 인수가 제공됩니다.

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>'; 
    } 
    } 
+1

방금 ​​소스의 일부를 게시 했으므로 정의가 if 문 또는 실행되지 않은 루프에있을 수 있습니다. 그 문제가 꽤 많이있는 – Benny

+0

@ 베니를 찾으십시오. 나는 브래킷이나 콜론을 찾고 있다고 생각합니까? 아니면 정확히 무엇을 찾아야할까요? 내 코드 스 니펫의 '...'은 긴 코드 목록과 같은 것일 뿐이며, 대괄호 등을 포함하지 않으며, 내가 의미하는 바를 알면 코드를 더 작게 만듭니다. 여러분이 보는 구조는 풀 코드 – user2048994

+0

과 완전히 같은 구조입니다. 예를 들어,'$ questions'의 모든 항목을 찾으십시오. 또는 중요한 부분과 적절하게 건너 뛰었 기 때문에 적어도 여기에 넣으십시오. 또는 while ($ selectedstudentanswerstmt-> fetch())'가 적어도 한 번 반복되었는지 확인하십시오. 그렇지 않으면 질문이 null이 될 수 있습니다. – Kyborek

답변

0

자체가 될 수있는 변수 $ 질문의 초기화 : 그 이하 나는 코드의 하단에, 최대한 줄이기 위해 노력했다

코드가되는 것은 오류 라인이 주석하는 곳이다 문제. 코드를보고 $questions =과 같은 것이 어디에 있는지 확인하십시오. 아마도 if가 실행되지 않을 때마다 매번 실행됩니다.

당신은 호출하는 경우 :

$questions[...] 

결코 당신이 또한 문제가있는 것입니다 배열로 $questions을 초기화. 당신이 배열의 필드를 액세스 아무런 문제가되지 않습니다이 후

$questions = array(); 

: 그래서 당신과 함께 $questions를 초기화해야합니다.

+0

'$ questions [...]'일부 버전에서는 "문제가있을 수 있습니다"라고 말하고 싶습니다. PHP는 예상대로 작동하지만 일부에서는 예기치 않게 작동합니다. – Kyborek

+0

예, 확실하지만 왜 초기화해야합니까?)하지만 힌트를 주셔서 감사합니다! – Benny

+0

@Benny where where 나는 $ questions = array();'위의 while 루프는 첫 번째 if 문 위에 while 루프에서 데이터를 출력하지 않기 때문에 여전히 빈 배열을 초기화하기 때문에 – user2048994