mysqli causes courseid to not be found at all

I have a problem with my code below. When the code below was old mysql code, it worked perfectly as the user is able to enter in a courseid in a textbox and if the courseid in the textbox matches with a courseid in the database, it will display the courseid and course name else if it is not in the database, then it will display a message stating it can't find the course id.

But since I tried changing the code from mysql to mysqli, then no matter if the courseid I enter into the textbox is correct or not, it keeps stating the message that the courseid cannot be found, why is this?

Below is the code (I obviously connected to the database:

 <?    

  $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'];

    }


        ?>

Answers


You're not having much luck with the mysqli statements are you? Here is your code revised with a (hopefully) working example. You still made a few mistakes, which I highlighted within the code. Please refer to the CourseId column in your db, is this an int or 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>

Need Your Help

Generating keyPair using Bouncy Castle

java c#-4.0 windows-phone-8 bouncycastle

I have Java code for generating keypair using BC as follows:

Get XSLT to place XML data in correct columns

xml xslt xslt-1.0

I was wondering if an xslt guru could me solve this problem.