<?php     //start php tag
//include connect.php page for database connection
Include('dbConnection.php');
//if submit is not blanked i.e. it is clicked.
// var_dump($_REQUEST);
session_start();

$errCount = 0;
$user = $_POST['usernameTbx'];
$pass = $_POST['passwordTbx'];
if(isset($_REQUEST['submit'])!='')
{
    //VALIDATE USERNAME TEXTBOX
    if($_REQUEST['usernameTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('usernameTbx' => "Username cannot be empty");

    }
    //VALIDATE PASSWORD TEXTBOX
    if($_REQUEST['passwordTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('passwordTbx' => "Password cannot be empty");
    } 

    if($errCount == 0) {
        $sql=mysql_query("Select username, password FROM user_tbl WHERE username = $user AND password = $pass");

        $row = mysql_fetch_array($sql);
        if($row["username"]==$user && $row["password"]==$pass)
        {
            $_SESSION['sysMsg'] = array('$strSuccess' => "You have successfully logged in!" );

        }
        else
        {
            $_SESSION['sysMsg'] = array('$strFail' => "Incorrect Username or Password" );
        }
    }
    header('Location: loginPage.php');
}

?>

This code always throws $strFail @_@. Can somebody tell me why? Thank you in advance.

Try this, if there's an error, paste it here.

$sql = mysql_query("Select username, password FROM user_tbl WHERE username = '$user' AND password = '$pass'") or die(mysql_error());
commented: die(mysql_error()) helped me +2

it says there was a problem on where clause so i change my sql code to this:

$sql=mysql_query("Select username, password FROM user_tbl WHERE username = '".$_REQUEST['usernameTbx']."' AND password = '".$_REQUEST['passwordTbx']."'") or die(mysql_error());

I don't know what happen but it's already working. Thanks a lot!

<?php     //start php tag
//include connect.php page for database connection
Include('dbConnection.php');
//if submit is not blanked i.e. it is clicked.
// var_dump($_REQUEST);
session_start();
$user = $_POST['usernameTbx'];
$pass = $_POST['passwordTbx'];
$errCount = 0;

if(isset($_REQUEST['submit'])!='')
{
    //VALIDATE USERNAME TEXTBOX
    if($_REQUEST['usernameTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('usernameTbx' => "Username cannot be empty");

    }
    //VALIDATE PASSWORD TEXTBOX
    if($_REQUEST['passwordTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('passwordTbx' => "Password cannot be empty");
    } 

    if($errCount == 0) {
        // $sql=mysql_query("Select username, password FROM user_tbl WHERE username = '".$_REQUEST['usernameTbx']."' AND password = '".$_REQUEST['passwordTbx']."'") or die(mysql_error());
        $sql = mysql_query("Select username, password FROM user_tbl WHERE username = '$user' AND password = '$pass'") or die(mysql_error());

        $row = mysql_fetch_array($sql);
        if($row["username"]==$user && $row["password"]==$pass)
        {
            $_SESSION['sysMsg'] = array('$strSuccess' => "You have successfully logged in!" );

        }
        elseif($row["username"]!=$user && $row["password"]!=$pass)
        {
            $_SESSION['sysMsg'] = array('$strFail' => "Incorrect Username or Password" );
        }
    }
    header('Location: loginPage.php');
}

?>

Try This

<?php     //start php tag
//include connect.php page for database connection
Include('dbConnection.php');
//if submit is not blanked i.e. it is clicked.
// var_dump($_REQUEST);
session_start();
$errCount = 0;
$user = $_POST['usernameTbx'];
$pass = $_POST['passwordTbx'];
if(isset($_REQUEST['submit'])!='')
{
    //VALIDATE USERNAME TEXTBOX
    if($_REQUEST['usernameTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('usernameTbx' => "Username cannot be empty");
    }
    //VALIDATE PASSWORD TEXTBOX
    if($_REQUEST['passwordTbx']=='') {
        $errCount += 1;
        $_SESSION['error'] += array('passwordTbx' => "Password cannot be empty");
    } 

        $sql=mysql_query("Select username , password FROM user_tbl WHERE username ='".$user."' AND password = '".$pass."'");
        $row = mysql_fetch_array($sql); 
        if($row["username"]==$user && $row["password"]==$pass)
        {
            $_SESSION['sysMsg'] = array('$strSuccess' => "You have successfully logged in!" );
        }
        else
        {
            $_SESSION['sysMsg'] = array('$strFail' => "Incorrect Username or Password" );
        }

    header('Location: loginPage.php');
}
?>
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.