I have my MySQL database connected to my php code (it works just fine, as I use it at a few other places), and I have a registration/login page. Registration works fine as well, I can see all the details in my database. However, I can't login.
if (isset($_POST['login_btn'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($password)) { array_push($errors, "Password is required"); }
if (empty($errors)) {
$password = md5($password);
$stmt = $conn->prepare("SELECT * FROM users WHERE username='$username' and password='$password' LIMIT 1");
$stmt->execute();
$result = $stmt->get_result();
if (mysqli_num_rows($result) > 0) {
$reg_user_id = mysqli_fetch_assoc($result)['id'];
$_SESSION['user'] = getUserById($reg_user_id);
if ( in_array($_SESSION['user']['role'], ["Admin", "Author"])) {
$_SESSION['message'] = "You are now logged in";
header('location: ' . BASE_URL . '/admin/dashboard.php');
exit(0);
} else {
$_SESSION['message'] = "You are now logged in";
header('location: index.php');
exit(0);
}
} else {
array_push($errors, 'Wrong credentials');
}
}
}
My code always goes to the else branch ("Wrong credentials"). Looks like it never gets my user row in the first place. I am quite stuck on this one.