1
<!DOCTYPE HTML>
<html>
<head>
<title>Sign-In</title>
</head>
<body id="body-color">
<div id="Sign-In">
<form method="POST" action="connectivity.php">
User <br><input type="text" name="user" size="40"><br>
Password <br><input type="password" name="pass" size="40"><br>
<input id="button" type="submit" name="submit" value="Log-In">
</form>
</fieldset>
</div>
</body>
</html> 

The above is the html code of my form which I have used for simple user login.

<?php
define('DB_HOST', 'localhost:3306');
define('DB_NAME', 'sample');
define('DB_USER','user');
define('DB_PASSWORD','passwd');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function SignIn()
{
session_start();   //starting the session for user profile page
if(!empty($_POST['user']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
    $query = mysql_query("SELECT *  FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
    if(!empty($row['userName']) AND !empty($row['pass']))
    {
        $_SESSION['userName'] = $row['pass'];
        echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";

    }
    else
    {
        echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...";
    }
}
}
if(isset($_POST['submit']))
{
    SignIn();
}

?>

This is the php script I have used to verify a user's login. But the above php code responds correctly when I entered the correct username and password which is stored previously in database. But when I tried to enter the wrong username and password, the else part is not working. Instead it is showing a white screen. Where can I've made the mistake?

Can anyone help me in getting this problem fixed?

2 Answers 2

1

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows

So check for FALSE not empty.

 if(($row['userName'])!==FALSE AND ($row['pass'])!==FALSE)
Sign up to request clarification or add additional context in comments.

Comments

0

Your code fails when the mysql_fetch_array returns false. Simply, remove empty from your if statement, and you will check if the query returned one or more records and filled the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.