以下のコードに問題があります。以下のコードが古い mysql コードの場合、ユーザーはテキスト ボックスに courseid を入力でき、テキスト ボックス内の courseid がデータベース内の courseid と一致する場合は、courseid とコース名が表示されるため、完全に機能しました。データベースにない場合は、コース ID が見つからないというメッセージが表示されます。
しかし、コードをmysqlからmysqliに変更しようとしたので、テキストボックスに入力したコースIDが正しいかどうかに関係なく、コースIDが見つからないというメッセージが表示され続けます。これはなぜですか?
以下はコードです(明らかにデータベースに接続しました:
<?
$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'];
}
?>