无法读取 table(SQL 和 PHP)

Cannot read from table (SQL and PHP)

我一直在尝试为我的网站创建登录表单,但该表单似乎无法连接到 table 或从中检索信息。我什至尝试在线获取一些示例代码,但仍然无法连接。这是代码:

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); 
    }
}    

无论我插入什么值,我都会收到错误代码 "Username or Password is invalid"。 table 确实包含值,即使我正确插入值,我也会收到此错误。我假设它无法连接到数据库或 table。有什么想法吗?

编辑: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>

因为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); 
    }
}  
?>  

这里存在问题:-

$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); 
    }
}  
?>