I am new in PHP and MySQL and I am making a login script for an admin but I can't seem to find the problem with the code below. The username and password is still incorrect even though it exists in the database and it was entered correctly in the form.
<?php
if(isset($_POST['adminlogin-submit'])){
require 'dbh.inc.php';
$username = $_POST['adminusername'];
$password = $_POST['adminpassword'];
if(empty($username) || empty($password)){
header("Location: ../adminlogin.php?error=emptyfields");
exit();
}
else{
$sql = "SELECT * FROM admin WHERE username=?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: ../adminlogin.php?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($result)){
$passwordCheck = password_verify($password, $row['password']);
if($passwordCheck == false){
header("location: ../adminlogin.php?error=wrongpassword");
exit();
}
else if($passwordCheck == true){
session_start();
$_SESSION['id'] = $row['id'];
$_SESSION['adminusername'] = $row['username'];
header("location: ../adminlogin.php?login=success");
exit();
}
else{
header("location: ../adminlogin.php?error=unknownerror");
exit();
}
}
else{
header("location: ../adminlogin.php?error=nouser");
exit();
}
}
}
}
else{
header("location: ../adminlogin.php");
exit();
}
