I am making a website and login feature which is connected to a database. I have written the script and it does login and logout but it just log in even if the form fields are empty for example if I don't put anything in username and password field it still logs in. I have checked and tested my database is connected with the PHP file on the server. I have tried so many things after researching online but all waste of my time and I still am unable to get it working properly,
my code is :
<?php
if (isset($_POST['loginsubmit'])){
$query = "SELECT user_id, password FROM users WHERE username = '".$_POST['username']."'";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);
if ($row['password'] == $_POST['pword']){
$_SESSION['id'] = $row['user_id'];
$_SESSION['loggedin'] = true;
}else{
$_SESSION['id'] = 0;
$_SESSION['loggedin'] = false;
}}
if (isset($_SESSION['loggedin'])==true){
echo "<p> Hello " . "$_POST[username]"." <a href='logout.php'>LogOut </a> </p>";
}else {
echo "<p>You are NOT logged in</p>\n";
}
What I am looking to do is to check:
- A username has been entered in the form
- A password has been entered in the form
- The username/password combination entered in the form are correct and user actually exists in the database.