0

我正在尝试检索从课程下拉模块中选择的课程的详细信息,然后显示一个模块下拉菜单,其中列出了属于该课程的所有模块。

我遇到的问题是,可以说我的课程下拉菜单中有这两个选项:

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>  
4

1 回答 1

0

尝试交换: foreach ($dataArray as $course => $courseData) for foreach ($dataArray as $foundCourse => $courseData)

然后$courseforeach 中的所有 s 也应该重命名。您使用页面顶部的变量$course,即使它不能解决问题,它也会使代码更易于理解。

您还应该考虑使用 PDO 将您的数据库提供程序抽象出来http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated

编辑:

代码说“这里是所有课程,现在如果你提交使用 $course 并获取它的详细信息”

但是您永远不会得到发布的课程,所以它会(恰好发生)使用从foreach脚本顶部“剩余”的相同命名变量。

我建议按照我上面说的做,然后在 if(isset($_POST['submit'])) 添加 之后$submittedCourseId = $_POST['coursesDrop'];

然后将其添加到您的绑定中 $qrystmt->bind_param("s",$submittedCourseId);

我认为这应该解决问题。

于 2012-10-25T17:19:31.143 回答