我正在尝试编写用于创建登录页面的PHP代码。检查,双重检查和三重检查我的代码后,我发现它们没有任何问题。但是,每次执行我的代码时,我都会收到错误消息:
表' company.login'不存在 警告:mysqli_num_rows()期望参数1为mysqli_result,第26行的C:\ xampp \ htdocs \ login100.php中给出布尔值
我知道当错误被称为" login100.php"的第26行时,并不一定意味着该行本身有任何问题。这是" login100.php":
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and
//password as a parameter
$connection = mysqli_connect("localhost", "root", "", "Company");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
// SQL query to fetch information of registerd users and finds user match.
$sqli = "SELECT * FROM login where password='$password' AND
username='$username'";
$result = mysqli_query($connection, $sqli);
if ($result === false) {
echo mysqli_error($connection);
}
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile100.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
}
}
?>
第26行对应$ rows = mysqli_num_rows($ result);正如您所看到的,没有什么不对,因为很容易看出您只需要一个参数,而不是两个参数。实际的错误与表&#34;登录&#34;的事实有关。不存在。这让我很烦恼,因为我确信它确实如此。让我告诉你&#34; session100.php&#34;它创建了一个名为&#34; login&#34;:
的表 <?php
// Establishing Connection with Server by passing server_name, user_id
// and password as a parameter
$connection = mysqli_connect("localhost", "root", "", "Company");
// Selecting Database
//$db = mysqli_select_db("company", $connection);
//CREATE DATABASE company;
$sql="CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id))";
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysqli_query("select username from login where
username='$user_check'", $connection);
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysqli_close($connection); // Closing Connection
header('Location: index100.php'); // Redirecting To Home Page
}
?>
正如您所看到的,没有任何故障。显示登录页面的方法是运行&#34; index100.php&#34;在XAMPP上。 &#34; index100.php&#34;的代码如下所示。我的问题是,你们中的任何一个人都可以发现&#34; session100.php&#34;如上所示,因为在我的眼中,一切都是桃子的。我希望额外的一双眼睛可能证明我错了,并告诉我哪里错了。
&#34;的index.php&#34;:
<?php
include('login100.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: profile100.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style100.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>