0

In the following php script when correct un and pass were given, it displays Welcome.But when we give wrong pass as shown in the code, it doesn't output "Wrong username password combination".what is the reason for that?

<?php
$query = mysql_query($sql);
if (!$query)
{
    echo 'Invalid query: ' . mysql_error() . "\n";
    die($message);
}
if (mysql_fetch_assoc($query))
{
    $query = mysql_query($sql);
    $row   = mysql_fetch_assoc($query);
    if ($row['un'] == $un && $row['pd'] == $encpass)
    {
        echo "<h1>WellCome</h1>";
        echo '<br /><a href="home.php?' . SID . '">page 2</a>';
        echo $row['un'];
    }
    else
    {
        echo "Wrong user name password combination";
    }
}
?>
1
  • 1
    please indent your code. You can use tools like this if your code really does look like that. Commented Jun 26, 2013 at 10:01

2 Answers 2

2

$Since you check for the right password in the select there will not be any row to fetch if the password is wrong, therefore you will not enter the code after mysql_fetch_assoc.

Try

if($row = mysql_fetch_assoc($query)) {
  echo "<h1>WellCome</h1>";
  echo '<br /><a href="home.php?' . SID . '">page 2</a>';
  echo $row['un'];
} else {
  echo "Wrong user name password combination";
}
Sign up to request clarification or add additional context in comments.

Comments

2

try

...
if(mysql_fetch_assoc($query)) {
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
if($row['un'] == $un && $row['pd'] == $encpass) {
    echo "<h1>WellCome</h1>";
    echo '<br /><a href="home.php?' . SID . '">page 2</a>';
    echo $row['un'];
} else
    echo "Wrong user name password combination";
} else
echo "Wrong user name password combination";
...

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.