我自己学习PHP。我正在使用超薄框架与 Medoo 并创建登录页面,并且在执行登录页面时没有错误。但在致电" call_query
"发生功能错误说:
未定义的变量:数据库中 第8行的C:\ xampp \ htdocs \ class \ _blog \ pages \ login.php
和
致命错误:在非对象中调用成员函数select() 第8行的C:\ xampp \ htdocs \ class \ _blog \ pages \ login.php
在代码中,我创建了一个对象$database
,然后使用select
方法。
<?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>';
?>
答案 0 :(得分:0)
M aking $database
。
谢谢 @lmarcelocc,@ jeroen,@ MD。 Sahadat Hossain。
现在代码是:
<?php
require_once 'medoo.php';
$database = new medoo();
session_start();
$login_error = $br = null;
function call_query($username, $password){
global $database, $login_error, $br;
$data = $database->select('user', 'user_name', ['user_name'=>$username, 'password' => $password]);
if (!empty($data)) {
print_r($data);
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location: localhost/class/blog");
}
}
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']) {
$_SESSION['username'] = $_POST['u_name'];
$_SESSION['password'] = $_POST['pass'];
header("location: /");
echo "logged by session";
}else{
call_query($_POST['u_name'], $_POST['pass']);
$login_error = "Invalid Username or Password.";
$br = "<br><br>";
}
}else{
call_query($_POST['u_name'], $_POST['pass']);
$login_error = "Invalied Username or Password.";
$br = "<br><br>";
}
}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>';
?>