我对PHP比较陌生,但我知道一些。我正在尝试创建一个登录系统,但登录功能似乎不起作用。我正在做一个check rows命令并检查它是否为= 1,但是它说它不是=到1。 “警告:mysql_num_rows()期望参数1是资源,第25行的C:\ xampp \ htdocs \ message \ login.php中给出的对象”
我知道这是因为它没有找到匹配的值,但我检查了数据库,用户名和密码肯定在那里。
非常感谢任何帮助。
代码如下:
<html>
<head>
<title>Login - Messaging</title>
</head>
<body>
<?php include "connect.php"; ?>
<?php include "functions.php"; ?>
<?php include "header.php"; ?>
<div>
<form method="post">
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if($username === '' || $password === ''){
$message="One or more of the fields are empty";
} else {
$password1=md5(hash("sha512",$password));
$checklogin = mysqli_query($con,"SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password1'");
$check2=mysql_num_rows($checklogin);
if($check2==1){
$message="Successful!";
header("location: index.php");
} else {
$message="Incorrect Username or Password";
}
}
echo "<p>$message</p>";
}
?>
Username: <input type="text" name="username" /><br>
Password: <input type="password" name="password" /><br>
<input type="submit" name="submit" value="Login" />
</form>
</div>
</body>
</html>
答案 0 :(得分:1)
您将mysql_*
与mysql_i*
混合,您需要检查查询错误。
更改这些行:
$checklogin = mysqli_query($con,"SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password1'");
$check2=mysql_num_rows($checklogin);
为:
$checklogin = mysqli_query($con,"SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password1'") or die(mysqli_error($con));
$check2=mysqli_num_rows($checklogin);