0

我的代码有问题。当下面的代码是旧的 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'];

    }


        ?>
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 回答