简体   繁体   中英

PHP login function not finding correct username and password

    // query
     $result = $server->prepare("SELECT * FROM admins WHERE username= :hjnji AND password=     :asos");
     $result->bindParam(':hjnji', $user);
     $result->bindParam(':asos', $password);
     $result->execute();
     $rows = $result->fetch(PDO::FETCH_NUM);
     if($rows > 0) {
     header("Location: database.php");
     }

I'm trying to create a login function, to my database to the table "admins". However, whenever I try to login into my database through a browser, it's echoing my error message that it doesn't have the correct username and password. I'd imagine it's something to do with my query not connecting to the database, but at the moment I'm all out of ideas.

Your code can be simplified, for one I'm not sure about the extra spaces in your query or the usage of "PDO::FETCH_NUM". I would propose the following.

$query = $server->prepare("SELECT * FROM admins WHERE username = ? AND password = ?");
$query->execute(array($username, $password));

if($query->rowCount() == 1) {
    // Username / Password correct

    header("Location: database.php");
    exit();
} else {
    // Username / Password incorrect
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM