2
下面我有一個PHP腳本,它顯示「課程」下拉菜單和「模塊」下拉菜單。假定發生的事情是用戶首先從「課程」下拉菜單中選擇一門課程,然後屬於所選課程的模塊列表將出現在「模塊」下拉菜單中。下面是該代碼:它一直顯示不正確的模塊名稱和編號,如何解決?
create_session.php
$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())
{
$courseno = $dbCourseNo;
$course = $dbCourseId;
$coursename = $dbCourseName;
$courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;
}
$courseHTML .= '</select>';
?>
<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 = $_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]['CourseNo'] = $dbCourseNo;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo;
// session data
$_SESSION['idcourse'] = $dbCourseNo;
$_SESSION['namecourse'] = $dbCourseName;
$_SESSION['idmodule'] = $dbModuleNo;
$_SESSION['namemodule'] = $dbModuleName;
}
foreach ($dataArray as $foundCourse => $courseData) {
$output = "";
$output .= "<p><strong>Course:</strong> " . $courseData['CourseNo'] . " - " . $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'>" . $moduleData['ModuleNo'] . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;
}
}
$moduleHTML .= '</select>';
echo $output;
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $moduleHTML; ?>
<input id="courseSubmit" type="submit" value="Submit" name="submit" />
</th>
</tr>
</table>
</form>
因此,可以說我在接下來的「課程」的課程INFO101 - Information Communication Technology
下拉菜單中,它顯示在模塊以下模塊下拉菜單下面對應與課程:
CHI2520 - Advanced Web Programming
CHI2220 - Systems Strategy
CHI2350 - Interactive Systems
現在這是我的問題。如果我選擇模塊CHI2520 - Advanced Web Programming
,然後訪問下面的頁面,它將顯示此模塊,而不是CHI2350 - Interactive Systems
。
QandATable.php:
<?php
if (isset($_POST['idmodule'])) {
$_SESSION['idmodule'] = $_POST['idmodule'];
}
if (isset($_POST['namemodule'])) {
$_SESSION['namemodule'] = $_POST['namemodule'];
}
if (isset($_POST['idcourse'])) {
$_SESSION['idcourse'] = $_POST['idcourse'];
}
if (isset($_POST['namecourse'])) {
$_SESSION['namecourse'] = $_POST['namecourse'];
}
$outputDetails = "";
$outputDetails .= "
<table id='sessionDetails' border='1'>
<tr>
<th>Course:</th>
<th>{$_SESSION['idcourse']} {$_SESSION['namecourse']}</th>
</tr>
<tr>
<th>Module:</th>
<th>{$_SESSION['idmodule']} {$_SESSION['namemodule']}</th>
</tr>
</table>
";
echo $outputDetails;
?>
我的問題是,爲什麼它顯示其他頁面錯誤模塊編號和名稱?這兩個頁面不包括session_start();
下面是形式,導航到QandATable.php:
<form action="QandATable.php" method="post" id="sessionForm">
<table><tr><th>6: Module:</th>
<td><?php echo $moduleHTML; ?></td>
</tr>
</table>
<p><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" /></p>
</form>
那麼我可以問爲什麼它會一直選擇它在下一頁顯示的模塊嗎?如果你知道我的意思,是否有這種行爲發生的某種原因?爲什麼它不顯示其他模塊? – user1819709
我猜它是在選擇你過去選擇的某個點,我不能確切地說出原因。我希望設置該屬性有所幫助。 – dgig
我會放棄一下,但你能否看到我的$ _SESSION變量是否有問題?我想要做的就是在會話變量中顯示'$ dbModuleNo和$ dbModuleName',然後使用它們顯示在下一頁 – user1819709