I am learning PHP by myself. I am using Slim framework with Medoo and creating a login page and when executing login page there is no error. but when calling "call_query
" function error occur saying :
Undefined variable: database in C:\xampp\htdocs\class\blog\pages\login.php on line 8
and
Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\class\blog\pages\login.php on line 8
In code i have created an object $database
then using select
method on it.
<?php
require_once 'medoo.php';
$database = new medoo();
session_start();
$login_error = $br = null;
function call_query(){
$data = $database->select('user', 'user_name', [
'user_name'=>$_POST['u_name'],
'password'=>$_POST['pass']
]);
if (count($data) == 1) {
header("location : localhost/class/blog");
}else{
$login_error = "Wrong Username or Password";
$br = "<br><br>";
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['submit'] == 'submit'){
if (isset($_POST['u_name']) && isset($_POST['pass']) && !empty($_POST['u_name']) && !empty($_POST['pass'])) {
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
if ($_SESSION['username'] == $_POST['u_name'] && $_SESSION['password'] == $_POST['pass']) {
header("location : localhost/class/blog");
}else{
call_query();
}
}else{
call_query();
}
}else{
$login_error = "Enter both Username and Password to login.";
$br = "<br><br>";
}
}
echo '<form action = "pages/login.php" method = "post">
<label>Username : </label><input type ="text" name ="u_name"><br><br>
<label>Password : </label><input type = "password" name = "pass"><br><br>
'.$login_error.$br.'
<input type ="submit" name = "submit" value = "submit">
</form>';
?>