其他部分在PHP脚本中不起作用

时间:2013-06-26 09:57:11

标签: php

在下面的php脚本中,当给出正确的un和pass时,它显示Welcome.But当我们给出错误的传递时,如代码所示,它不输出“错误的用户名密码组合”。这是什么原因?

<?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";
    }
}
?>

2 个答案:

答案 0 :(得分:2)

$由于您在select中检查了正确的密码,如果密码错误,将无法获取任何行,因此您不会在mysql_fetch_assoc之后输入代码。

尝试

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";
}

答案 1 :(得分:2)

尝试

...
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";
...