in my code below it is not able to recognise the variable $userid, this variable determines the id of the teacher logged in but in the mysqli code it is not able to determine the $userid when I echo it. But it does know the user is logged in by its $userid. So my question is that in the mysqli why is it unable to find the $userid? Please look at comment in mysqli bind_param() setion and this is where the problem is.
Below is the code, both php and mysqli:
Let me state that member.php is the script which contains the $userid info:
<?php
session_start();
include('member.php');
...
function PickSession()
{
//Get data from database
$sessionquery = "
SELECT s.SessionId, SessionName, s.TeacherId
FROM Session s
INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
WHERE
(ModuleId = ? AND Complete = ? AND s.TeacherId = ?)
ORDER BY SessionName
";
$complete = 1;
global $mysqli;
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("iii",$moduleId, $complete, $userid);//it doesn't recognse $userid
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbTeacherId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
echo $userid; //nothing displayed in this echo
...
if ((isset($username)) && (isset($userid))){ //user is successfully logged in
include('teachername.php');
...
<?php
ShowAssessment(); // Show information
}else{
echo "Please Login to Access this Page | <a href='./teacherlogin.php'>Login</a>";
}
?>
member.php script:
if (isset($_SESSION['teacherid'])) {
$userid = $_SESSION['teacherid'];
}
if (isset($_SESSION['teacherusername'])) {
$username = $_SESSION['teacherusername'];
}
?>
You have not initialised the
$useridvariable, you have to set the value for binding the variable in query otherwise it will takeNULLvalue for that variable.The
$useridvariable is not accessible inPickSession(). So the suggestion is either make$useridas global or pass it as parameter in function.