我的代码有问题。当下面的代码是旧的 mysql 代码时,它可以完美运行,因为用户可以在文本框中输入 courseid,如果文本框中的 courseid 与数据库中的 courseid 匹配,它将显示 courseid 和课程名称,否则它不在数据库中,然后它会显示一条消息,指出它找不到课程 ID。
但是由于我尝试将代码从mysql更改为mysqli,然后无论我在文本框中输入的courseid是否正确,它都会提示找不到courseid的消息,这是为什么?
下面是代码(我明明是连接数据库的:
<?
$courseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
?>
<h1>CREATING A NEW SESSION</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" /><input id="courseSubmit" type="submit" value="Submit" name="submit" /></p> <!-- Enter User Id here-->
</form>
<?php
if (isset($_POST['submit'])) {
$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("ss",$courseid);
// get result and assign variables (prefix with db)
$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
$num = $qrystmt->num_rows($result = $qrystmt->execute());
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else {
$dataArray = array();
while ($row = $qrystmt->fetch()) {
$dataArray[$row['CourseId']]['CourseName'] = $row['CourseName'];
$dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName'];
$_SESSION['idcourse'] = $row['CourseId'];
$_SESSION['namecourse'] = $row['CourseName'];
}
?>