I have started to learn php and mysql and I have created a login form. I want to show error when username or password is incorrect. But when the page is opened it shows Notice: Undefined index: user and Undefined index: pass, and i have included the php code below the password input box because I want to show the error below it, but I keep getting this error.And if I enter any wrong username and click on login then this error doesn't appear. Please help
<html>
<head>
</head>
<body>
<form action="login.php" method="post">
<label for="username">Username</label>
<input name="user" type="text" id="username" required="" >
<label for="password">Password</label>
<input name="pass" type="password" id="password" required="" >
<?php
// start session
session_start();
include'connect.php';
// Define $username and $password
$username=$_POST['user'];
$password=$_POST['pass'];
$sql="SELECT * FROM table WHERE username='$username' and password='$password'";
$result=mysqli_query($con,$sql);
// Mysql_num_row is counting table row $count=mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
$_SESSION['username'] = $_POST['user'];
// goto to welcome page
header('Location: welcome.php');
}
else {
// show error
echo "<div style='color:red;' class='errorbox'>Incorrect Username or Password</div><br>";
}
?>
<button type="submit">Log In</button>
</form>
</body>
</html>