此php登录代码始终不返回任何内容

时间:2019-01-14 04:18:52

标签: php mysql forms passwords md5

操作文件

function userLogin($username,$pass){
    $password = md5($pass);
    $stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
    $stmt->bind_param("ss",$username, $password);
    $stmt->execute();
    $stmt->store_result();
    return $stmt->num_rows > 0;
}

function getUserByUsername($username){
    $stmt = $this->con->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->bind_param("s",$username);
    $stmt->execute();
    return $stmt->get_result()->fetch_assoc();
}

用户登录文件

require_once('../includes/DbOperations.php');

$response = array();

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    if (isset($_POST['username']) and isset($_POST['password'])) {
        $db = new DbOperations();

        if ($db->userLogin($_POST['username'], $_POST['password'])) {
            $user = $db->getUserByUsername($_POST['username']);
            $response['error'] = false;
            $response['id'] = $user['id'];
            $response['email'] = $user['email'];
            $response['username'] = $user['username'];
        }else{
            $response['error'] = true;
            $response['message'] = "Invalid username or password";
        }

    }else{
        $response['error'] = true;
        $response['message'] = "Required fields are missing";
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下代码

function userLogin($username,$pass){
    $password = md5($pass);
    $stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
    $stmt->bind_param("ss",$uname, $pwd);
    $uname = $username;
    $pwd = $password;
    $stmt->execute();
    $stmt->store_result();
    return $stmt->num_rows > 0;
}

function getUserByUsername($username){
    $stmt = $this->con->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->bind_param("s",$uname);
    $uname = $username;
    $stmt->execute();
    return $stmt->get_result()->fetch_assoc();
}