gpt4 book ai didi

php - undefined variable 和mysqli错误

转载 作者:行者123 更新时间:2023-12-02 11:01:02 25 4
gpt4 key购买 nike

我在代码中遇到了这些错误,但我不知道为什么

错误:

1. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 28
2. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 28
3. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 29
4. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 29
5. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 31
6. mysqli_query() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 31
7. mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 32
8. mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 33

这是我的文件

dbcon.php
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

function getConnected($hostname, $username, $password, $database) {
$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con;}?>

index.php

<?php session_start(); ?>
<?php include('dbcon.php');?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="form-wrapper">

<form action="#" method="post">
<h3>Влез в системата</h3>

<div class="form-item">
<input type="text" name="user" required="required" placeholder="Фак. Номер" autofocus required/>
</div>

<div class="form-item">
<input type="password" name="pass" required="required" placeholder="Парола" required/>
</div>

<div class="button-panel">
<input type="submit" class="button" title="Log In" name="login" value="Влез"/>
</div>
</form>
<?php
if (isset($_POST['login']))
{
$username = mysqli_real_escape_string($con, $_POST['user']);
$password = mysqli_real_escape_string($con, $_POST['pass']);

$query = mysqli_query($con, "SELECT * FROM users WHERE password='$password' and username='$username'");
$row = mysqli_fetch_array($query);
$num_row = mysqli_num_rows($query);

if ($num_row > 0)
{
$_SESSION['user_id']=$row['user_id'];
header('location:home.php');

}
else
{
echo 'Invalid Username and Password Combination';
}
}
?>
<div class="reminder">
<p>Not a member? <a href="#">Sign up now</a></p>
<p><a href="#">Forgot password?</a></p>
</div>
</div>
</body>
</html>


我得到了mysqli_query(),mysqli_fetch_array()和mysqli_num_rows()的那些错误。他们都期望参数1为mysqli_result,为null。

最佳答案

dbcon.php文件中,您具有getConnected($hostname, $username, $password, $database)函数,但从未使用过。您应该在 index.php中的代码中进行如下操作:

$con = getConnected($hostname, $username, $password, $database);

它将如下所示:
if (isset($_POST['login'])) {
$con = getConnected($hostname, $username, $password, $database);
$username = mysqli_real_escape_string($con, $_POST['user']);
$password = mysqli_real_escape_string($con, $_POST['pass']);
// ........ rest of your code

或者,您可以将这些行写出一个函数:
$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error) // should be mysqli_connect_errno()
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con; // without return

因此 dbcon.php 如下所示:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error) // should be mysqli_connect_errno()
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

同样在您的函数( dbcon.php )中,检查是否已建立连接被写入错误,应该如下所示:
if (mysqli_connect_errno()) {
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

您正在使用 过程样式,可以找到更多信息 here

关于php - undefined variable 和mysqli错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40438151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com