无法从表中读取(SQL和PHP)


Cannot read from table (SQL and PHP)

我一直在尝试为我的网站创建一个登录表单,但表单似乎无法连接到表或从中检索信息。我甚至尝试在网上获得一些示例代码,但它仍然无法连接。下面是代码:

session_start(); 
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is empty";
    } else {    
        $username=$_POST['username'];
        $password=$_POST['password'];
        $connection = mysqli_connect("localhost", "user", "pass");
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($username);
        $password = mysqli_real_escape_string($password);   
        $db = mysqli_select_db($connection, "cpses_company");
        $query = mysqli_query("select * from login where password='$password' AND username='$username'", $connection);
        $rows = mysqli_num_rows($query);
        if ($rows == 1) {
            $_SESSION['login_user']=$username; 
            header("location: profile.php"); 
        } else {
            $error = "Username or Password is invalid";   
        }
        mysqli_close($connection); 
    }
}    

无论我插入什么值,我总是得到错误代码"用户名或密码无效"。表确实包含值,即使在正确插入值时也会出现此错误。我假设它无法连接到数据库或表。什么好主意吗?

编辑:HTML (index.php)

<?php
include('login.php'); // Includes Login Script
if(isset($_SESSION['login_user']))
{
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>
</html>

这里存在问题:-

$query = mysqli_query("select * from login where password='$password' AND username='$username'", $connection);

你需要像这样把$connection作为第一个参数:-

$query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'");

注意:-尽量使用mysql error reporting,这样你就可以摆脱你所面临的问题。为此,您需要像下面这样做,非常简单:-

$query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'")or die(mysqli_error($connection));

还有一些其他的问题,所以为了你的帮助,请尝试这样做:-

<?php
session_start(); 
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is empty";
    } else {    
        //$username=$_POST['username'];
        //$password=$_POST['password'];
        $connection = mysqli_connect("localhost", "user", "pass","cpses_company"); // direct give db name here
 // remove that two line what i said in comment also
        $username = mysqli_real_escape_string($connection,$_POST['username']);
        $password = mysqli_real_escape_string($connection,$_POST['password']);   

        $query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'") or die(mysqli_error($connection));
        //$rows = mysqli_num_rows($query);//comment this line
        if ($query->num_rows > 0) {
            $_SESSION['login_user']=$username; 
            header("location: profile.php"); 
            exit;
        } else {
            $error = "Username or Password is invalid";   
        }
        mysqli_close($connection); 
    }
}  
?>  

mysqli_query需要这样的参数:-

mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

读mysqli_query

所以你的mysqli_query将是:-

先参数connection再参数query

$query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'");

<?php
session_start(); 
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is empty";
    } else {    
        $username=$_POST['username'];
        $password=$_POST['password'];
        $connection = mysqli_connect("localhost", "user", "pass","cpses_company"); // direct give db name here
 // remove that two line what i said in comment also
        $username = mysqli_real_escape_string($connection,$username);
        $password = mysqli_real_escape_string($connection,$password);   

        $query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'") or die(mysqli_error($connection));
        //$rows = mysqli_num_rows($query);//comment this line
        if ($query->num_rows > 0) {
            $_SESSION['login_user']=$username; 
            header("location: profile.php"); 
        } else {
            $error = "Username or Password is invalid";   
        }
        mysqli_close($connection); 
    }
}  
?>