I have a website I'm trying to add multiple admins to. When logging in, only the first username/password combination in the database works and any others just throw the WRONG INFORMATION error message in the code below. Seems like a problem with the foreach maybe? I'm new to PHP and not sure how else to do it:
<?php
session_start();
include_once('connection.php');
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"]== "POST") {
$adminname = test_input($_POST["adminname"]);
$password = test_input($_POST["password"]);
$stmt = $conn->prepare("SELECT * FROM adminlogin");
$stmt->execute();
$users = $stmt->fetchAll();
foreach($users as $user) {
if(($user['adminname'] == $adminname) &&
($user['password'] == $password)) {
$_SESSION["authenticated"]="test";
header("Location: adminpage");
}
else {
echo "<script language='javascript'>";
echo "alert('WRONG INFORMATION')";
echo "</script>";
die();
}
}
}
?>
Thank you!