2012-06-13 70 views
0

我的下面的代碼有問題。當下面的代碼是舊的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']; 

    } 


     ?> 
+0

陣列(「courseid」)?這是正確的?我的意思是,你正在創建數組還是使用內部有數組的鍵? – 2012-06-13 00:42:50

+0

是的,這個我我怎麼用舊的MySQL,它運行良好,這是一個與mysqli的問題? – user1394925

+0

完全不同於mysqli,不相關,但很奇怪,因爲你在循環中創建它,這意味着一個循環。當然不是數組鍵?例如$ _POST [「course_id」]?不知道它 – 2012-06-13 01:19:26

回答

1

你不必與mysqli的聲明多少運氣,你在這裏是你的代碼(希望)工作示例修訂。你還是犯了一些錯誤,這是我的代碼中突出顯示。請參考您的分貝courseId列,這是一個int或char類型?

<?php 
//........ 
// SESSION/DB Connection   
//........ 

// Don't do a foreach loop on variables that you can explicitly create 
$courseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : ''; 
$foundResult = false; 

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); 
    // only one 's' as there is only one variable 
    $qrystmt->bind_param("s",$courseid); 
    // execute query 
    $qrystmt->execute(); 
    // get result and assign variables (prefix with db) 
    $qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName); 
    // Store the result (so num_rows can be calculated) 
    $qrystmt->store_result(); 
    // set a bool for results recieved (not really neccessary but to keep with your code) 
    $foundResult = ($qrystmt->num_rows > 0) ? true : false; 

    // if a result is found process the results 
    if ($foundResult) { 

     // are you expecting more than one course to be retrieved? 
     // if so why only one session for a single course? 
     $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; 
     } 
    } 

    /* 
    * Good practise to free result/close connection if not doing anymore 
    * processing with mysqli - otherwise exclude the below statements 
    */ 
    // Free the stmt result 
    $qrystmt->free_result(); 

    // Close statement 
    $qrystmt->close(); 
} 
?> 
<html> 
<head></head> 
<body> 
    <?php if ($foundResult == false && $_POST) { 
    echo "<p>Sorry, No Course was found with this Course ID " . htmlentities($courseid,ENT_QUOTES,'UTF-8') . "</p>"; 
    } 
    ?> 
    <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> 
</body> 
</html> 
+0

嗨再次史蒂夫,courseid是一個varchar – user1394925

+0

好吧有更新數據庫代碼來處理varchar,讓我知道你是如何得到上。 –

+0

剛剛注意到(現在已更正),我把$ stmt-> execute()而不是$ qrystmt-> execute(); (現在更正了)。所有人都應該現在工作。 –