下面我有一个查询,它执行查询以从数据库中查找所有课程详细信息并将其显示在下拉菜单中,然后使用$outputcourse
变量在消息中显示所选课程:
$sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseNo";
$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;
$outputcourse = "";
$hiddencourse = "";
while($sqlstmt->fetch())
{
$course = $dbCourseId;
$courseno = $dbCourseNo;
$coursename = $dbCourseName;
$courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;
if (isset($_POST['courses']) && ($_POST['courses'] == $course)) {
$outputcourse .= "<p><strong>Course:</strong> " . $courseno . " - " . $coursename . "</p>";
$hiddencourse .= "<p><input type='hidden' id='hiddencourse' value='". $courseno . " - " . $coursename ."'></p>";
}
}
$courseHTML .= '</select>';
现在我要做的是显示不属于所选课程的模块。现在我的查询在 php 中运行良好,但我遇到的问题是,如果我使用 mysqli 运行此查询,它无法$dbCourseId
在 where 子句中检索变量,因此 WHERE 子句不正确。
WHERE 子句应该显示应该是的 courseId1
但它一直显示10
。如果我选择 courseId 应该在的不同课程3
,它仍然会输出10
。
那么我的问题是,哪个变量应该放在下面代码中的 bind->param() 中,我怎样才能撤销所选课程的正确 courseId?
下面是查询的代码:
$modulequery = "
SELECT
m.ModuleId, m.ModuleNo, m.ModuleName, m.Credits
FROM
Module m
WHERE
m.ModuleId NOT IN (
SELECT cm.ModuleId
FROM Course_Module cm
WHERE cm.CourseId = ?
)
ORDER BY m.ModuleNo
";
$moduleqrystmt=$mysqli->prepare($modulequery);
// You only need to call bind_param once
$moduleqrystmt->bind_param("i",$dbCourseId);
// get result and assign variables (prefix with db)
$moduleqrystmt->execute();
$moduleqrystmt->bind_result($dbModuleId,$dbModuleNo,$dbModuleName,$dbCredits);
$moduleqrystmt->store_result();
$modulenum = $moduleqrystmt->num_rows();
echo $dbCourseId;