从php开始,我编写了一个基本的身份验证脚本,如果"test“数据库的user表中存在一个userid (由user提供),该脚本将打印出mysql服务器上的数据库列表。
问题是,即使数据库中不存在用户if,此脚本也会输出数据库列表。我不确定这个脚本出了什么问题。请看一下脚本,并帮助我理解为什么输出数据库列表,即使数据库中不存在用户in。下面是脚本:
<?php
if(isset($_POST['submitted']))
{
$userid=$_POST['userid'];
$userpassword=$_POST['userpassword'];
$link_id=mysql_connect("localhost","root","pass");
$result_db_list=mysql_list_dbs($link_id);
mysql_select_db("test",$link_id);
if(!($result_ptr=mysql_query("Select userid from user where Userid='$userid'",$link_id))) die ("Please enter correct userid");
while($test=mysql_fetch_row($result_db_list))
{
echo $test[0]."<br>";
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Authentication Script</title>
<style type="text/css" >
#header{
padding-top:0px;
margin:0px;
background-color:#CCCCCC;
}
.container{
width:950px;
margin:0 auto;
border:1px solid red;
}
.authbox {
padding-top:100px;
margin:0 auto;
}
#footer{
background-color:#666666;
color:white;
}
</style>
</head>
<body>
<div id="header">
<div class="container">
<form action="authentication script.php" method="post">
<div class="authbox">UserName: <input type="text" name="userid" /><br/>
Password: <input type="password" name="userpassword" /><br/>
<input type="hidden" name="submitted" value="true" />
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
<div id="footer">
Copywright 2010 NT Technologies.
</div>
</body>
</html>
<?php
}
?>谢谢,rseni。
发布于 2011-06-01 23:48:31
你的脚本充满了错误。(我希望你至少有magic_quotes on,否则你就有大麻烦了。请注意,无论如何都应该避免使用magic_quotes,并使用预准备语句)
这是因为
if(!($result_ptr=mysql_query("Select userid from user where Userid='$userid'",$link_id)))
die ("Please enter correct userid");这就是说,如果查询没有选择任何内容,它就不会返回FALSE。
您应该将其更改为:
$result = mysql_query("SELECT COUNT(*) as countUser [etc]");
$r = mysql_fetch_assoc($result);
if ($r['countUser']==0)
die('Denied');发布于 2011-06-01 23:57:51
Appu - yes123是正确的。查看有关mysql_query函数的php.net文档-您将看到,如果成功,它将返回一个资源标识符;如果出错,它将返回FALSE。Error在这里并不意味着没有返回任何行-而是一个错误,比如您试图对一个不存在的表运行此查询。
https://stackoverflow.com/questions/6203923
复制相似问题