creating a log in page using php and PDO however once I fill out the form with the correct username and password to log into the system and click log in, nothing changes except the boxes are now empty as if I never entered a username and password, can someone advise me as to what is going wrong?
this is the php code:
<?php
session_start();
$server = "127.0.0.1";
$dbusername = "root";
$dbpassword = "";
$db = "movie1";
$message = "";
try
{
$handle = new PDO("mysql:host=$server; dbname=$db", $dbusername, $dbpassword);
$handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["login"]))
{
if(empty($_POST["username"]) || empty($_POST["password"]))
{
$message = '<label>All fields are required</label>';
}
else
{
$query = "SELECT * FROM register WHERE username = :username AND password = :password";
$statement = $handle->prepare($query);
$statement->execute(
array(
'username' => $_POST["username"],
'password' => $_POST["password"]
)
);
$count = $statement->rowCount();
if($count > 0)
{
$_SESSION["username"] = $_POST["username"];
header("location:login_success.php");
}
else
{
$message = 'Wrong Data';
}
}
}
}
catch(PDOException $error)
{
$message = $error->getMessage();
}
?>
and this is the html form to which it applies to:
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>log in</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<form class="w3-container w3-card-4" action="login.php" method="post">
<h2 class="w3-text-black">Log in</h2>
<?php
if(isset($message))
{
echo '<label class "text-danger">'.$message.'</label>';
}
?>
<p>
<label class="w3-text-black"><b>username</b></label>
<input class="w3-input w3-border" name="username" type="text" placeholder="username"></p>
<p>
<label class="w3-text-black"><b>Password</b></label>
<input class="w3-input w3-border" name="password" type="text" placeholder="********"></p>
<p>
<input type="submit" name="login" class="w3-btn w3-black">Log in</input>
</p>
<p>
please ignore that passwords are not hashed, I will fix that later. the first image is what the log in form looks like with information entered and the next will be what happens after I click log in (pic one)1
pic two 2