我正在尝试检索从课程下拉模块中选择的课程的详细信息,然后显示一个模块下拉菜单,其中列出了属于该课程的所有模块。
我遇到的问题是,我可以说在我的课程下拉菜单中有这两个选项:
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>
答案 0 :(得分:0)
尝试交换:
foreach ($dataArray as $course => $courseData)
对于
foreach ($dataArray as $foundCourse => $courseData)
然后也应该重命名foreach中的所有$course
。您可以在页面顶部使用变量$course
,即使它无法解决问题,也会使代码更容易理解。
您还应该考虑使用PDO来抽象数据库提供程序http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated
编辑:
代码说“这里是所有课程,现在如果你提交使用$ course并获得它的详细信息”
但是在任何时候你都没有得到发布的课程,所以它会(恰好这样)使用与脚本顶部的foreach
相同的命名变量“leftover”。
我建议按照我上面说的那样做
if(isset($_POST['submit']))
加
$submittedCourseId = $_POST['coursesDrop'];
然后将THAT添加到绑定中
$qrystmt->bind_param("s",$submittedCourseId);
我认为应该解决问题。