I'm trying to check if a users entered username and password matches an entry from the database using PDO, but for some reason cannot seem to get it working.
The database table is called user_info and the rows from which i'm trying to grab the username and password from are username
and pass
respectively. I'm not entirely sure how to check if the users input information matches a database entry, then make a decision on where to send them based on if it returns true or false.
Also, i've obviously got the prerequisites such as including a file that connects to the database, which i know whose information is correct.
I've had a stab in the dark on the if ($username)
etc, but clearly it's incorrect.
HTML
<form method="post">
<label for="username2">Username</label>
<input type="text" name="username">
<label for="password">Password</label>
<input type="text" name="password">
<input type="submit" value="Login" name="login">
</form>
PHP
$username = $_POST['username'];
$password = $_POST['password'];
try {
$result = $db->prepare("SELECT * FROM user_info WHERE username = :user AND pass = :pass");
$result->bindParam(':user', $username);
$result->bindParam(':pass', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
}
catch (Exception $e) {
echo "Could not retrieve data from database";
exit();
}
if ($username == "test" && $password == "test") {
$_SESSION['username'] = $username;
redirect('/add-property.php');
} else {
if (isset($_POST['login'])) {
echo "Username or password incorrect";
}
}