gpt4 book ai didi

php - 当我在循环中添加一个额外的字段时,while 循环不起作用

转载 作者:行者123 更新时间:2023-11-29 03:07:09 24 4
gpt4 key购买 nike

我有以下代码:

 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;

}

完整代码如下,它从数据库中选择一门类(class),然后选择与所选类(class)相对应的模块:

 $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;

...

最佳答案

当你打电话时

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

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

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

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

关于php - 当我在循环中添加一个额外的字段时,while 循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13533302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com