I'm just learning the PHP basics and I need to create a blog. I've followed a few tutorials but I can't seem to be able to get PHP to recognize when I've pressed the login form I created with HTML.
<?php
session_start(); //keep a session open so no need to relogin
if(isset($_POST['submit'])){ //check to see if submit button is pressed
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
//add database connection
include('./includes/db_connect.php');
if(empty($user) || empty($pwrd)){
echo 'Please ensure both password and username fields are filled.';
}else{
$pwrd = md5($pwrd);
$query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password = '$pwrd'"); //grab username and password from table
if($query->num_rows ===1) {
while ($row = $query->fetch_object()) {
$_SESSION['user_id'] = $row->user_id;
}
header('Location : index.php'); //redirt user to index.php
exit();
}else{
echo 'Wrong credentials';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="container">
<form action="login.php" method="post">
<p>
<label>Username</label><input type="text" name="username" />
</p>
<p>
<label>Password</label><input type="password" name="pwrd" />
</p>
<input type="submit" value="LogIn" />
</form>
</div>
</body>
</html>
When I press log in while both username and password are empty I get nothing instead of 'Please ensure both password and username fields are filled.'
Any help would be appreciated.