2

我在下面有这段代码:

 while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseNo;
          $_SESSION['namecourse'] = $dbCourseName;

    }

现在,当我尝试在 while 循环中显示 2 个字段时,上面的代码可以正常工作。但是当我尝试在 while 循环中添加第三个字段时,它似乎不起作用,并且它给了我一个错误说明 undefined ModuleName

是不是我布置的方式导致了下面的while循环中的错误:

while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseNo;
          $_SESSION['namecourse'] = $dbCourseName;

    }

完整的代码在下面,它从数据库中选择一门课程,然后选择与所选课程对应的模块:

 $sql = "SELECT CourseId, CourseNo, CourseName FROM Course"; 

 $sqlstmt=$mysqli->prepare($sql);

 $sqlstmt->execute(); 

 $sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

 $courses = array(); // easier if you don't use generic names for data 

 $courseHTML = "";  
 $courseHTML .= '<select name="courses" id="coursesDrop">'.PHP_EOL; 
 $courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) 
 {   
     $courseid = $dbCourseId;
     $course = $dbCourseNo;
     $coursename = $dbCourseName; 
     $courseHTML .= "<option value='".$courseid."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;  
  } 

  $courseHTML .= '</select>'; 

    ?>

        <?php
if (isset($_POST['submit'])) {

$submittedCourseId = $_POST['courses'];

    $query = "
                 SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
                 c.CourseName,
                 m.ModuleName
                 FROM Course c
                 INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
                 JOIN Module m ON cm.ModuleId = m.ModuleId
                 WHERE
                 (c.CourseId = ?)
                 ORDER BY c.CourseName, m.ModuleId
                ";

    $qrystmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $qrystmt->bind_param("s",$submittedCourseId);
    // get result and assign variables (prefix with db)

    $qrystmt->execute(); 

    $qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName);

     $qrystmt->store_result();

    $num = $qrystmt->num_rows();

    if($num ==0){
        echo "<p style='color: red'>Please Select a Course</p>";
    } else { 

        $dataArray = array();

 while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseNo;
          $_SESSION['namecourse'] = $dbCourseName;

    }

     foreach ($dataArray as $foundCourse => $courseData) {

          $output = ""; 

          $output .= "<p><strong>Course:</strong> " . $foundCourse .  " - "  . $courseData['CourseName'] . "</p>";

       $moduleHTML = ""; 
       $moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
       $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId => $moduleData) {        

            $moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;        
  } 
            }
            $moduleHTML .= '</select>';

      echo $output;

      ...
4

3 回答 3

1

你打电话时

$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName);

您正在为 $dbModuleNo 分配一行。所以它可能是一个值而不是一个数组。当您调用时,这会导致问题:

$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName; 

$dbModuleNo(可能)没有为“ModuleName”定义。

于 2012-11-23T17:27:23.760 回答
0

您是否尝试在循环之前使用数组值的回显来测试数组?也许您正在尝试为未明确定义的变量分配值。

于 2012-11-23T17:25:30.763 回答
0

看起来您从未将 $dbModuleNo 定义为数组。在循环之前添加 $dbModuleNo = array() 。

于 2012-11-23T17:28:00.863 回答