登录脚本不会登录并显示错误

时间:2014-04-10 08:19:34

标签: php mysql

我使用以下登录脚本,我收到此错误,它不会登录。

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\hewden_spms\login.php on line 41

我的数据库:

id | username | password

1     mark      password

脚本:

<html>
    <head>

    </head>
    <body>

 <?php

 $host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="hewden1"; // Database name 
$tbl_name="spms.admin_members"; // Table name 



// Connect to server and select databse.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");



// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 



// To protect MySQL injection (more detail about MySQL injection)
 $myusername = stripslashes($myusername);
 $mypassword = stripslashes($mypassword);
 $myusername = mysql_real_escape_string($myusername);
 $mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
 $result=mysql_query($sql);



// Mysql_num_row is counting table row
 $count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
 session_register("myusername");
 session_register("mypassword"); 
header("location:dashboard.php");
 }
 else {
 echo "Wrong Username or Password";
 }
 ?>



     <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
 <tr>
<form name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>" >
 <td>
 <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
 <tr>
 <td colspan="3"><strong>Member Login </strong></td>
 </tr>
 <tr>
 <td width="78">Username</td>
 <td width="6">:</td>
 <td width="294"><input name="myusername" type="text" id="myusername"></td>
 </tr>
 <tr>
 <td>Password</td>
 <td>:</td>
 <td><input name="mypassword" type="text" id="mypassword"></td>
 </tr>
 <tr>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td><input type="submit" name="Submit" value="Login"></td>
 </tr>
 </table>
 </td>
</form>
</tr>
 </table>   


</body>

</html>

1 个答案:

答案 0 :(得分:0)

如果它们位于同一页面中,则脚本会在第一页加载时立即尝试阅读$_POST['myusername']$_POST['mypassword'] - 当它们尚未发布时。我将所有PHP放在一个块中,检查(isset($_POST['myusername']))或其他一些已发布的变量。这样,只有在您实际发布数据后,PHP才会尝试登录。

不相关,我建议转移到mysqli_个功能,因为MySQL_现已弃用。