0

以下のコードに問題があります。以下のコードが古い 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'];

    }


        ?>
4

1 に答える 1

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>
于 2012-06-13T01:29:34.463 に答える