我正在尝试检索从课程下拉模块中选择的课程的详细信息,然后显示一个模块下拉菜单,其中列出了属于该课程的所有模块。
我遇到的问题是,可以说我的课程下拉菜单中有这两个选项:
INFO101 - Business
INFO102 - ICT
出于某种奇怪的原因,每次我从下拉菜单中选择顶部选项(INFO101)并单击提交按钮时,它总是显示其他课程详细信息(INFO102),从而显示属于该课程而不是其他课程的模块课程。
我的问题是,当我从下拉菜单中提交(INFO101)选项时,为什么它会显示其他课程的信息?
下面是mysqli代码
$sql = "SELECT CourseId, CourseName FROM Course";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->execute();
$sqlstmt->bind_result($dbCourseId, $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())
{
$course = $dbCourseId;
$coursename = $dbCourseName;
$courseHTML .= '<option value="'.$course.'">' . $course . ' - ' . $coursename . '</option>'.PHP_EOL;
}
$courseHTML .= '</select>';
$courseHTML .= '</form>';
?>
<?php
include('noscript.php');
?>
<h1>CREATING A NEW ASSESSMENT</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?><input id="courseSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])) {
$submittedCourseId = (isset($_POST['courses']));
$query = "
SELECT cm.CourseId, cm.ModuleId,
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,$dbCourseName,$dbModuleName);
$qrystmt->store_result();
$num = $qrystmt->num_rows();
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$course'</p>";
} else {
$dataArray = array();
while ( $qrystmt->fetch() ) {
// data array
$dataArray[$dbCourseId]['CourseName'] = $dbCourseName;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName;
// session data
$_SESSION['idcourse'] = $dbCourseId;
$_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;
更新:
以下是视图页面源显示的内容:
<form action="/u0000000/Mobile_app/create_session.php" method="post">
<table>
<tr>
<th>Course: <select name="courses" id="coursesDrop">
<option value="">Please Select</option>
<option value='INFO101'>INFO101 - Bsc Information Communication Technology</option>
<option value='INFO102'>INFO102 - Bsc Computing</option>
</select></form><input id="courseSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
</table>
</form>
<p>Sorry, No Course was found with this Course ID 'INFO102'</p>