dshmkgq558192365 2010-02-26 13:06
浏览 11
已采纳

mysql_fetch assoc拒绝返回密码行。 需要帮助

The username row comes out perfectly but the password row refuses to come through.

I am at a loss here.

Does anybody know what the solution is?

here is my code::

<?php

//Mass include file
include ("includes/mass.php");

//This is the login script
//Grabbing the login values and storing them

$username = $_POST['username'];
$password = $_POST['password'];
$submit   = $_POST['submit'];

if (isset($submit))
{
    if (strlen($username)<2) // put || ($username==(same as value on the database)
    {
        echo ("<br>You must enter a longer username</br>");
    }
    elseif (strlen($password)<=6)
    {
        echo ("<br>You must enter a longer password<br>");
    }
    else
    {
        $sql = "SELECT * FROM user WHERE username = '$username'";
        $query = mysql_query($sql);
        $numrows = mysql_num_rows($query);

        if ($numrows != 0)
        {
            while ($row = mysql_fetch_assoc($query))
                $dbusername = $row['username'];
            $dbpassword = $row['password'];         

            if ($dbusername == $username && $dbpassword == $password)
            {
                echo "your in!";
            }
            else
            {
                echo "Wrong info";
            }
        }
        else
        {
            die ("That username doesnt exist");
        }
    }
}

?>
  • 写回答

3条回答 默认 最新

  • dongwu9972 2010-02-26 13:11
    关注

    You have a missing parenthesis after the while.

    Both

    $dbusername = $row['username'];
    $dbpassword = $row['password'];      
    

    Should be within the while loop which is not the case.

    You need to do something like:

    while ($row = mysql_fetch_assoc($query)) {
    
        $dbusername = $row['username'];
        $dbpassword = $row['password'];           
    
        if ($dbusername == $username && $dbpassword == $password) {
            echo "your in!";
        }
    ....
    

    Adding to what Matthew has said:

    You should not show messages like "username doesnt exist" to the user. This provides valuable information to a hacker who wants to break in. In case the user provides a wrong username and/or wrong password, you should display "invalid username or password".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数