I was wondering if someone can please help me sort out a problem I'm having with getting a piece of code to work? I've spent hours on it so far, and although it seems simple enough it just won't cooperate:(
It's a simple login page, and at the moment all I'm trying to do is check the inputted name and password against those stored in the database to see if they exist there, and grant access if they match. That part is working fine, but the elseif part that comes afterwards doesn't do anything at all.
The elseif is supposed to echo out a message that says their Username or Password don't match, but nothing happens - when you click submit with the incorrect details, the page seems to submit but the message doesn't appear. Could the issue be to do with the loop that it's all sitting in?
I'm sorry if this is a silly question or if it's been answered before, it's driving me a bit nuts.
Thank you very much in advance to anyone who can help me out, it's greatly appreciated:)
P.S. I've tried using (mysqli_num_rows($query) == 0) to check the database for the input values too but that doesn't seem to make a difference.
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($connection,$query);
while($row=mysqli_fetch_assoc($result)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
//Check to see if posted values equal db values
if ($dbusername == $username && $dbpassword == $password) {
echo "Hello " . $dbusername."!";
} //else, check to see that either of the posted values don't match db values
elseif ($dbusername !== $username || $dbpassword !== $password)
{
echo "Sorry, your username or password don't match";
} //end elseif
} //end while loop
} //end submit check