form validation issue

Hi guys, I am new here and this is my first post

I have an issue with validating the login form, whereas if the fields are empty then it should display a message next to the respective field. The login is working perfectly but I am havin an issue with validating the fields.

here’s the php login code.

[php]<?php

session_start();
$username = $_POST[‘username’];
if(empty($username))
{ echo(“Please Input UserName”); }
$password = $_POST[‘password’];
if(empty($password))
{ echo(“Please Input Password”); }

if(isset($_POST[‘admin’]) && $_POST[‘admin’]==“admin”)
{
$type=“admin”;
}
else
{
$type=“normal”;
}

$con = mysql_connect(“localhost”,“root”,“alpine”) or die (“Couldn’t connect”);
if($type==“admin”)
{
mysql_select_db(“database_acc”, $con);
$result = mysql_query(“SELECT * FROM admin”);
while($row = mysql_fetch_array($result))
{ if($row[‘user_id’]==$username && $row[‘password’]==$password) {
$_SESSION[‘user’]=$username;
$_SESSION[‘type’]=“admin”;
$acc=$acc+1;
header( ‘Location: admin.php’);
}
}
}
else
{
mysql_select_db(“database_acc”, $con);
$result = mysql_query(“SELECT * FROM members”);
while($row = mysql_fetch_array($result))
{
if($row[‘email’]==$username && $row[‘password’]==$password)
{
$_SESSION[‘user’]=$username;
$_SESSION[‘type’]=“normal”;
$acc=$acc+1;
header( ‘Location: members.php’);
}
}
}
if($flag==0)
{ header( ‘Location: index.php?invalidlogindetails=1’);
$_SESSION[‘details’]=“1”; }
?> [/php]

here’s the form code…
[php]<?php
session_start();
if(!isset($_SESSION[‘user’]) ) {
?>

E-Mail Address:

Password:

<?php } else echo " "; if(isset($_GET['details'])) { echo "Incorrect Login! "; } ?>
[/php]

I am using the isset function that has a variable in it… and if possible to carry that forward with empty fields would be great.

Thanks guys. jay :slight_smile:

In the first part of your code, the $flag variable is not set, so this condition is always true, and user will always be redirected:
[php]if($flag==0)
{ header( ‘Location: index.php?invalidlogindetails=1’);
$_SESSION[‘details’]=“1”; }[/php]

Also, at the top you have this code:
[php] if(empty($username))
{ echo(“Please Input UserName”); }
$password = $_POST[‘password’];
if(empty($password))
{ echo(“Please Input Password”); }[/php]

It not make sense to echo error message by this script, because you are redirecting user from that page, so user will not see error message, they will see php error “headers already sent”.

Sponsor our Newsletter | Privacy Policy | Terms of Service