我的下面的代碼有問題。當下面的代碼是舊的mysql代碼時,它可以很好地工作,因爲用戶可以在文本框中輸入courseid,並且如果文本框中的courseid與數據庫中的courseid相匹配,則會顯示courseid和課程名稱,如果不然它不在數據庫中,那麼它將顯示一條消息,指出它找不到課程ID。mysqli導致courseid根本找不到
但是,因爲我試着將代碼從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'];
}
?>
陣列(「courseid」)?這是正確的?我的意思是,你正在創建數組還是使用內部有數組的鍵? – 2012-06-13 00:42:50
是的,這個我我怎麼用舊的MySQL,它運行良好,這是一個與mysqli的問題? – user1394925
完全不同於mysqli,不相關,但很奇怪,因爲你在循環中創建它,這意味着一個循環。當然不是數組鍵?例如$ _POST [「course_id」]?不知道它 – 2012-06-13 01:19:26