0

Hello there, anyone can help me about my problem, i want to display an error message when the user inputs invalid username and incorrect password. and i want to display it in the same form. im new in php.

These are my codes:

<?php
	if($_SERVER['REQUEST_METHOD'] == 'POST')
	{
						$db=mysql_connect('localhost','root','') or die('Cannot connect to MySQL!');
@mysql_select_db('dbriomra')or die('Cannot connect to database');
 
// Retrieve username and password from database according to user's input
 
$login = mysql_query("SELECT * FROM login WHERE (username = '" . mysql_real_escape_string($_POST['compid']) . "') and (password = '" . mysql_real_escape_string($_POST['pword']) . "')");
 
// Check username and password match

if (mysql_num_rows($login) == 1) {
// Set username session variable

$_SESSION['username'] = $_POST['compid'];
// Jump to secured page
header('Location: employee.php'); 

// kung anong file.php mag ffall ung user pag valid ung log in procedures nya.
}
else {
$message = "Invalid username or password"; 
}
} else {
$message = "";
}
echo "$message";
?>
                
<?PHP print $message;?>
3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by shielaolid
1

put this above html

$user = $_POST['compid'];
$pass = $_POST['pword'];

$db=mysql_connect('localhost','root','') or die('Cannot connect to MySQL!');
@mysql_select_db('dbriomra')or die('Cannot connect to database');
session_start();

$sql = mysql_query("SELECT * FROM login WHERE username = '".$user."'");

if(mysql_num_rows($sql)>0){

	// login
	while($row=mysql_fetch_array($sql)){

		if($pass==$row['password']){
			// store id as session
			$_SESSION['id'] = $row['id'];
			echo "<script>window.location='/employee.php'</script>";
		}else{
		// password incorrect
			echo 'password incorrect';
		}

	}
}else{
	// fail
	echo "username doesn't exist";
}

then remove the action attribute in your form

Edited by vaultdweller123: n/a

0

vaultdweller123, Undefined compid and pword (that's the name of my textboxes).

and if when i input the correct username and password this is the error that i get

"The requested URL /employee.php was not found on this server."

employee.php is the page where the user go directly when they input correct username and password.

Edited by shielaolid: n/a

0

It's working.

<?php

error_reporting (E_ALL ^ E_NOTICE);
	
session_start();

$user = $_POST['txtcompid'];
$pass = $_POST['txtpword'];

$_SESSION['myusername'] = $user;
$_SESSION['mypassword'] = $pass;
 
$db=mysql_connect('localhost','root','') or die('Cannot connect to MySQL!');
@mysql_select_db('dbriomra')or die('Cannot connect to database');
session_start();
 
$sql = mysql_query("SELECT * FROM login WHERE username = '".$user."'");
 
if(mysql_num_rows($sql)>0){
 
	// login
	while($row=mysql_fetch_array($sql)){
 
		if($pass==$row['password']){
			// store id as session
			$_SESSION['id'] = $row['id'];
			header("location:employee.php");
			$_SESSION['username']=$user;
			
		}else{
		// password incorrect
			echo 'password incorrect';
		}
 
	}
}else{
	// fail
	echo "username doesn't exist";
}
?>

i just put a

$_SESSION['myusername'] = $user;
$_SESSION['mypassword'] = $pass;

to call my textboxes. and that's it. Thanks :) Have a nice day!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.