In my code below I am trying to retrieve the moduleid, use it to run a query in order to be able to find the module number and module name which belongs to that module id and then include session variables for the moduleid, module number and module name.
The problem I am having is that even though I am using (isset(), I am getting undefined index for $_SESSION['idmodule'] and ‘$_SESSION[‘namemodule’]. Now$_SESSION[‘module’]` is working fine, no problems with that session variable, but the other 2 session variables I am having problem with.
Now the query is correct so there is no problem wih the query, just the problem I am guessing on how I am trying to call the $_SESSION variables.
Below is the code:
if (isset($_POST['module']))
{
$query = "SELECT ModuleNo, ModuleName FROM Module WHERE ModuleId = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $_POST['module'] );
$stmt->execute();
$stmt->bind_result($moduleno, $modulename);
while ( $stmt->fetch() ) {
// session data
if (isset($moduleno)){
$_SESSION['idmodule'] = $moduleno;
}
if (isset($modulename)){
$_SESSION['namemodule'] = $modulename;
}
}
$_SESSION['module'] = $_POST['module'];
}
.
Both those statements may prevent
$_SESSION['idmodule']or$_SESSION['namemodule']to not be set, so when you reference them later you may get the warning.To prevent that, later in the code you may need this:
The beter way is to clean up a few things:
If the module is not found, it would be best to skip the rest of the code and just show an error message.