Currently trying to do a project for school and as far as php is concerned, the code is fine. When I run it, I enter the username and password and click the submit button, but nothing seems to happen, I remain on the login page.
<?php
session_start();
try{
$databaseConnection = new PDO('mysql:host=127.0.0.1;dbname=mycontacts','root','');
$databaseConnection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
if(isset($_POST['login'])) {
$errMsg = '';
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if ($username == ''){
$errMsg .= "You must enter your Username <br />";
}
if($password == '') {
$errMsg .= 'You must enter your Password<br>';
}
if ($errMsg == '') {
$records = $databaseConnection->prepare('SELECT id,username,password from users WHERE username = :username');
$records->bindParam(':username',$username);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
if(count($results) > 0 && password_verify($password, $results['password'])){
header('location:mainMenu.php');
exit();
}else {
$errMsg .= 'Username and Password are not found<br />';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="CSS/main.css">
</head>
<body>
<h1>Welcome to the Contact List</h1>
<br />
<br />
<div>
<form method="post" class="submit" name="login">
<h3><u>Please login</u></h3>
<p>Username: <input type="text" name="username" maxlength="15" size="20" /> </p>
<p>Password: <input type="text" name="password" maxlength="15" size="20" /> </p>
<input type="submit" value="Submit" name="login" />
</form>
</div>
</body>
</html>
The database name is mycontacts The table name is users The Fields are id(primary), username, password
When putting in the username and password, the screen will remain on the login with nothing in the textboxes, nor any errors when querying it.