我正在尝试做一个简单的登录脚本。也就是说,通过POST操作接受表单内容。检查数据库以查找匹配的记录。从该行中提取其他信息,例如Full Name
。
我的代码是;
if ( !isset($_POST['loginsubmit']) ) {
//Show login form
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
Account ID:
<input name="AccountID" type="text" />
</p>
<p>
Username:
<input name="userEmail" type="text" />
</p>
<p>Password:
<input name="userPassword" type="password" />
<p>
<input name="loginsubmit" type="submit" value="Submit" />
</p>
</form>
<?php
}
else {
//Form has been submitted, check for logon details
$sql = "SELECT * FROM users WHERE 'accountID'=". $_POST['AccountID']. " AND 'userEmail'=". $_POST['userEmail'] . " AND 'userPassword'=". $_POST['userPassword']. " LIMIT 1";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1){
echo"Correct Username/Password";
}
else {
echo "Wrong Username or Password";
}
}
我有两个问题。首先使用上面的代码,我不断收到以下错误。
警告:mysql_num_rows()期望参数1是资源,在...中给出布尔值
其次,如何从数据库中获取其他详细信息字段。我推测
$result=mysql_query($sql);
包含MySQL行的数组,所以我可以这样做;
echo $result['fullName'];
答案 0 :(得分:2)
首先清理字段以防止SQL注入。
$sanitize_fields = array('AccountID','userEmail','userPassword');
foreach( $sanitize_fields as $k => $v )
{
if( isset( $_POST[ $v ] ) )
$_POST[ $v ] = mysql_real_escape_string( $_POST[ $v ] );
}
然后引用查询中的字符串字段。最初,您的查询中存在错误。这就是你得到布尔值为false的原因。
$sql = "SELECT * FROM users WHERE accountID='". $_POST['AccountID']. "' AND userEmail='". $_POST['userEmail'] . "' AND userPassword='". $_POST['userPassword']. "' LIMIT 1";
我建议您在运行查询后执行以下操作,以查看MySQL生成的错误(如果有)。
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
MySQL扩展正在逐步淘汰,有更新的更好的扩展,如MySQLi和PDO,看看那些。
答案 1 :(得分:0)
在您的SQL语句中:
$sql = "SELECT * FROM users WHERE 'accountID'=". $_POST['AccountID']. " AND 'userEmail'=". $_POST['userEmail'] . " AND 'userPassword'=". $_POST['userPassword']. " LIMIT 1";
如果在表中,userEmail和userPassword是字符串,请添加单个qoutes:
$sql = "SELECT * FROM users WHERE accountID=". $_POST['AccountID']. " AND userEmail='". $_POST['userEmail'] . "' AND userPassword='". $_POST['userPassword']. "' LIMIT 1";
获得结果:
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
if(mysql_num_rows($result) > 0)
echo $row['COLUMN_NAME'];
}
}
你的代码非常不安全:
答案 2 :(得分:0)
试试这个:
else {
//Form has been submitted, check for logon details
$conn = mysql_connect("db-host-here","db-user-here","db-pass-here");
$sql = "SELECT * FROM users WHERE accountID=". mysql_real_escape_string($_POST['AccountID']). " AND userEmail='". $_POST['userEmail']=mysql_real_escape_string($_POST['userEmail']); . "' AND userPassword='". $_POST['userPassword']=mysql_real_escape_string($_POST['userPassword']);. "' LIMIT 1";
$result = mysql_query($sql,$conn);
$count = mysql_num_rows($result);
if($count == 1){
echo"Correct Username/Password";
}
else {
echo "Wrong Username or Password";
}
}
// Get other information:
$dbInfo = mysql_fetch_assoc(); //If more than one row can be selected, use a while loop.
//Now play with $dbInfo:
echo $dbInfo['some_other_column'];
您的查询中有单引号,而您不需要它们,而您在任何地方都错过了它们。试试上面的代码。
使用正确的数据库信息替换db-host-here
,db-user-here
和db-password-here
。
我已经在你的代码中做了一些转义,以防止注入攻击。但是你应该考虑使用准备好的语句。
答案 3 :(得分:0)
此处的问题是您的查询无法选择任何行,因此从FALSE
调用返回布尔值mysql_query
。
您应该修复您的查询并始终检查$result = mysql_query($query);
是否返回false,如下所示:
// ...
$result = mysql_query($query);
if($result !== false) {
$count = mysql_num_rows($result);
// ...
}
但我建议使用PDO或至少使用mysqli http://php.net/mysqli。