我对PHP和MySQL都很陌生,我很难让我的登录系统正常运行。注册工作正常,但是当我运行登录时,它无法识别表中是否有与输入数据匹配的内容。以下是我认为是问题区域的代码。
提前致谢。
<?php
function load($page = 'login.php')
{
$url = 'http://'.$_SERVER['HTTP_HOST'].
dirname($_SERVER['PHP_SELF']);
$url = rtrim($url,'/\/');
$url.= '/'.$page;
header("location:$url");
exit();
}
function validate($dbc,$email ='',$pwd='')
{
$errors = array();
if (empty($email))
{ $errors[] = 'Enter your email address.'; }
else
{ $e = mysqli_real_escape_string($dbc,trim($email));}
if (empty($pwd))
{ $errors[] = 'Enter your password.';}
else
{ $p = mysqli_real_escape_string($dbc, trim($pwd)); }
if (empty($errors))
{
$q = "SELECT adultID, FirstName, Surname "
. "FROM adult_information "
. "WHERE Email = '$e' AND Password = SHA1('$p')";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) == 1)
{ $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
return array( true, $row);}
else
{$errors[]='Email address and password not found.';}
}
return array(false,$errors);
}
答案 0 :(得分:0)
如果你改变了
,我相信你会得到你想要的东西$q = "SELECT adultID, FirstName, Surname "
. "FROM adult_information "
. "WHERE Email = '$e' AND Password = SHA1('$p')";
到
$p = SHA1($p);
$q = "SELECT adultID, FirstName, Surname "
. "FROM adult_information "
. "WHERE Email = '$e' AND Password = '$p'";
每当PHP-to-MySQL查询没有按预期执行时,我的第一步是查看我实际传递给数据库的SQL。在这种情况下,可以在分配$ q的值后立即插入echo '<p>$q</p>';
之类的行。
有时很明显我只是通过查看它就会出现格式错误的查询。如果没有,我将复制出现的SQL代码并将其作为数据库管理器中的查询运行,以查看它抛出的错误和/或检查结果数据。