I want to add a an error message if the user entered the wrong username or password. Currently if a username or password is wrong it just reloads the login page with no error.
Asked
Active
Viewed 5,533 times
0
-
I can't see any error messages given. Even you should write a else condition with an error message at all – Duke Feb 07 '12 at 04:26
-
@jacob Please see my answer. I have provided a link for a simple tutorial from creating table in the database, creating a HTML to PHP Script. – John Woo Feb 07 '12 at 11:16
4 Answers
0
Just modify your if statement to see if they had a successful login:
if( mysql_num_rows( $login) == 1)
{
// Successful login
$_SESSION['username'] = $_POST['username'];
...
}
else
{
// Invalid login
echo "Your username or password are incorrect!";
}

nickb
- 59,313
- 13
- 108
- 143
-
can you check if i have done it right and edit if necessary: http://pastebin.com/V1urcqBe – Jacob Feb 07 '12 at 05:42
-
@Jacob - It looks like you'll need something like this: http://pastebin.com/c9awk0X4 – nickb Feb 07 '12 at 06:20
-
i have edited my main post above as there was more to follow. maybe missing a brace – Jacob Feb 07 '12 at 07:07
0
if(mysql_num_rows($login) == 1)
{
$SESSION['username'=$_POST['username'];
//Successfull login redirect to home page
}
else
{
// incorrect username or password
$incorrectLogin_flag =1;
}
then in html
<?php
echo "Username : <input type='text' name='username' />";
echo "Username : <input type='password' name='password' />";
echo " <input type='submit' value='Login' />";
echo " <input type='submit' value='Cancel' />";
if($incorrectLogin_flag ==1)
{
echo" <label style='color:red;'> Username or Password incorrect.</label>";
}
?>

Naveen Kumar
- 4,543
- 1
- 18
- 36
0
Use else,
if(Something goes wrong){
//display the errors
}elseif(all goes right){
//login
//redirect
}
No need to give you the full code you can google it.

tomexsans
- 4,454
- 4
- 33
- 49
0
this may be the basic tutorial to create a simple validation using php and mysql.
REFERENCE for PHP Login script tutorial
Sample Code Snippet:
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// 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['myusername'];
$mypassword=$_POST['mypassword'];
// 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:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

John Woo
- 258,903
- 69
- 498
- 492