0

Below I have a mysqli query which does successfully work;

$courseid = (isset($_POST['courses'])) ? $_POST['courses'] : ''; 
$moduleid = (isset($_POST['moduleid'])) ? $_POST['moduleid'] : '';  
$moduleno = (isset($_POST['moduleno'])) ? $_POST['moduleno'] : '';  
$modulename = (isset($_POST['modulename'])) ? $_POST['modulename'] : '';

$query = "SELECT cm.CourseId, CourseNo, CourseName, cm.ModuleId 
FROM  Course c 
INNER JOIN Course_Module cm 
ON
c.CourseId = cm.CourseId
JOIN Module m
ON
cm.ModuleId = m.ModuleId
WHERE cm.CourseId = ? AND cm.ModuleId = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ii", $courseid, $moduleid);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbModuleId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();

The problem I am getting though is in my success message. It is able to output the $moduleno and $modulename variables but it does not output the $dbCourseNo and $dbCourseName variables in the success message. Why is this?

if ($numrows == 1){

echo "<span style='color: green'>The following Module has been added into Course:" . $dbCourseNo . " - " . $dbCourseName . ":<br/>" . $moduleno . " - " . $modulename . "</span>";

}
4

1 に答える 1

0

You never actually fetched the result, and I suspect the variables that do show are stale from a previous operation.

$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbModuleId);

// Can't remember, but you may need to call store_result() before bind_result() ??
// maybe not...
$stmt->store_result();
$numrows = $stmt->num_rows();
// Fetch the row...
$stmt->fetch();
// Now your variables are populated.
于 2012-12-13T00:49:38.457 に答える