-2

常量仅包含数据库连接详细信息

require_once 'includes/constants.php';
class mysql{
    private $conn;

    function __construct(){
        $this->conn = $conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)
            or die ('There was an error in the connection');
        }

    function verify ($un, $pwd){


             $query = "SELECT * FROM User WHERE username = ? AND pass = ? LIMIT 1"; 

             if ($stmt = $this->conn->prepare($query))
             {
                 $stmt->bind_param('ss', $un, $pwd);
                 $stmt->execute();

                 if ($stmt->fetch());{
                     $stmt->close();
                     return true;
                     }
             }
        }

    }

?>

un 和 pwd 已从登录页面传递,它们是从用户输入中提取的,我遇到的问题是用户始终登录,无论他们输入什么,它似乎都没有针对数据库正确验证

4

2 回答 2

0

改变这个

if ($stmt->fetch());{
   $stmt->close();
   return true;
}

 if ($stmt->fetch()){
   $stmt->close();
   return true;
}else
{ return false;}
于 2013-08-23T13:47:06.440 回答
0
if ($stmt->fetch());{
                   ^------- THIS!
    $stmt->close();
    return true;
}

接下来if是一个空语句,之后是一个新{...}的语句块。if仅与空语句相关联,因此始终执行以下块。

为了显示:

if (false)
    /* do nothing */;

{
    echo 'always goes here';
}

去掉多余的;

于 2013-08-23T13:49:40.320 回答