我是编程新手,所以请提前道歉,因为滥用我可能混淆的任何条款。
我正在尝试使用php和mysql创建一个非常简单的登录应用程序。有3个文件:config.php,connection.php和login.php。我正在使用Postman来测试我的输入和输出。但是,当我尝试运行php脚本时,它会给我这个结果(更新):
我不确定发生了什么,我已经尝试按照youtube上的教程检查我是否遗漏了一些但却找不到它..这是我的代码:
的config.php:
<?php
define('hostname', 'localhost:80');
define ('username', 'root');
define ('password', 'root');
define ('dbname', 'finger');
?>
connection.php:
<?php
require_once 'config.php';
class DB_connection{
private $connect;
function __construct(){
$this -> connect = mysqli_connect(hostname, username, password, dbname) or die( "DB Connection error");
}
public function get_connection(){
return $this-> connect;
}
}
?>
的login.php:
<?php
require_once 'connection.php';
class User{
private $db, $connection;
function __construct(){
$this->db = new DB_connection();
$this -> connection = $this -> db -> get_connection();
}
public function does_user_exist($email, $password){
$query = "Select * from users where email = '$email' and passwords = '$password'";
$result = mysqli_query($this->connection, $query);
if (mysqli_num_rows($result) > 0) {
$json['success'] = 'Sign in success!';
$json_encode ($json);
mysqli_close($this ->connection);
#return true;
}
else {
$json['failure'] = 'Sign in Failure, check credentials!';
$json_encode ($json);
mysqli_close($this ->connection);
#return false;
}
}
}
$user = new User();
if (isset ($_POST['email'], $_POST ['password'] )){
$email = $_POST['email'];
$password = $_POST['password'];
if (!empty ($email) && !empty($password)){
$encrypted_password= md5($password);
$user -> does_user_exist($email, $encrypted_password);
}
else{
echo json_encode(" Please enter both fields! ");
}
}
?>
答案 0 :(得分:-1)
您使用错误的方法获取电子邮件和密码的值。
如果你想使用Post方法,那么值将通过Body传递。但是您在查询字符串中传递了电子邮件和密码的值。
请使用$ _GET而不是$ _POST。
喜欢......
$user = new User();
if (isset ($_GET['email'], $_GET ['password'] )){
$email = $_GET['email'];
$password = $_GET['password'];
if (!empty ($email) && !empty($password)){
$encrypted_password= md5($password);
$user -> does_user_exist($email, $encrypted_password);
}
else{
echo json_encode(" Please enter both fields! ");
}
}