Trying to create simple PHP login form that takes 'username' and 'password', if matched with 'dbusername'
and 'dbpassword
' the code should echo "You're Logged In!.
When I run the code, I get no errors.The page goes from login.php
to process.php
but shows a blank page. Doesn't show: echo "Incorrent username or password!" or "You're Logged In!".
I checked to see if its returning any rows from database. I'm getting 0 rows. But why?! Is my code logic incorrect? Because my database connection works AND I have a username: alex and password: abc
in my database named phplogin
and in table users
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'phplogin';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT *
FROM users
WHERE username = '".$username."'
AND password = '".$password."'";
$result = mysqli_query($conn, $sql);
echo mysqli_num_rows($result); // I Checked to see if I was getting no rows. *I am getting 0 rows!!* But why?!
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
//check to see if the match
if($username == $dbusername && $password == $dbpassword ) {
echo "You're Logged In!";
} if($username != $dbusername || $password != $dbpassword) {
echo "Incorrect password or username!";
} else {
die("That user doesn't exist!");
}
}
}
mysqli_close($conn);
?>
Here is my login.php page [form]
<html>
<form action="process.php" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Log In"><br>
</html>
Any ideas?
UPDATE 1: The issue was with my variables conflicting with database.
Now I am getting the following once I login and it has to do with the last statement on my process.php page:
Why is the final else statement printing on screen when Its logging in?