我是SQL的新手,我正在尝试建立一个登录系统。我已经按照指南进行了操作,但是当我尝试登录时,我收到了这两条消息:
警告:mysqli_fetch_array()要求参数1为mysqli_result,第13行的C:\ xampp \ htdocs \ loginguide / login.php中给出布尔值
警告:mysqli_num_rows()要求参数1为mysqli_result,第16行的C:\ xampp \ htdocs \ loginguide \ login.php中给出布尔值
我在MyPHPAdmin上创建了一个数据库但是,我不知道如何正确地做到这一点,我是不是想在这个数据库中添加表?
这是login.php代码:
<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
session_register("myusername");
$_SESSION['login_user'] = $myusername;
header("location: welcome.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>
<head>
<title>Login Page</title>
<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
</style>
</head>
<body bgcolor = "#FFFFFF">
<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
<div style = "margin:30px">
<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>
<div style="font-size:11px;<?php echo $error; ?></div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
1)您需要在SQL查询中包含“active”列,或者只需将'id'替换为*
select * from admin where ....
(or)
select id, active from admin where ....
2)检查您在给定示例中包含的config.php文件,如果DB_USERNAME,DB_PASSWORD,DB_DATABASE名称错误(语法错误),您可能会收到该警告。
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'testDB');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
答案 1 :(得分:0)
我认为您的查询失败并返回false值。检查您的查询,如果它正常工作,请将其放在您的代码中:
$result = mysqli_query($db,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($db));
exit();
}
了解更多信息。
答案 2 :(得分:-1)
您只选择ID,然后使用&#34;有效&#34;
所以你应该先解决这个问题:
SELECT * FROM admin WHERE username = '$myusername' and passcode = '$mypassword'
它应该现在可以使用