<?php 
 // if there are any errors, display them
 $error='';
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="dbriomra"; // Database name
$tbl_name="login"; // Table name
$error='';
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
// username and password sent from form
$myusername=$_POST['compid'];
$mypassword=$_POST['pword'];

 
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count==1)
{
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:employee.php");
}
else 
{
$error = 'ERROR: Please fill in all required fields!';
}
?>

if the user login with incorrect employee no and password, the message must be show. but it doesn't. please help. Thanks

You are initializing the $error variable to an empty string just before checking whether it contains anything. Initialize it on the beginning of the script and do the check after the checking for correct password.

Also session_register function is deprecated. I recommend you avoid it. Use direct assignment instead:

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.