我的代码如下:
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循环中添加第三个字段时,它似乎不起作用并且它给出了一个错误,指出未定义的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;
...
答案 0 :(得分:1)
致电时
$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName);
您正在为$ dbModuleNo分配一行。所以它可能是一个值而不是数组。当您致电时,这会导致问题:
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName;
$ dbModuleNo(可能)没有为'ModuleName'定义。
答案 1 :(得分:0)
您是否尝试在循环之前使用数组值的回声测试数组? 也许你正试图将一个值赋值给一个定义不明确的变量。
答案 2 :(得分:0)
看起来您从未将$ dbModuleNo定义为数组。在循环之前添加$ dbModuleNo = array()。