目标: 我正在创建一个php登录脚本。
问题: 我似乎无法让我的包识别$ connection变量,即使它应该在connection.php中明确定义。结果,我的变量的值是/ NULL。
我尝试了什么: 我从mysql开始,但很快发现这是错误的方法并将我的代码转换为mysqli。我检查了所有$连接变量中的拼写错误。我确保路径是正确的。作为最后的手段,我进行了谷歌搜索,但没有找到答案或任何有用的暗示我的方案。
问题: 我的变量未被定义的原因是什么?
错误消息:
所有这些消息都与此单个变量无关,因为某些原因没有定义:
Php注意:未定义的变量:第4行的C:\ xampp \ htdocs \ aspie \ Php \ Core \ Common.php中的连接
Php警告:mysqli_real_escape_string()期望参数1为mysqli,在第4行的C:\ xampp \ htdocs \ aspie \ Php \ Core \ Common.php中给出null
Php注意:未定义的变量:第4行的C:\ xampp \ htdocs \ aspie \ Php \ Core \ Functions \ Members.php中的连接
Php警告:mysqli_query()期望参数1为mysqli,在第4行的C:\ xampp \ htdocs \ aspie \ Php \ Core \ Functions \ Members.php中给出
Php警告:mysqli_num_rows()要求参数1为mysqli_result,在第5行的C:\ xampp \ htdocs \ aspie \ Php \ Core \ Functions \ Members.php中给出null
// INITIALIZER
<?php
session_start();
// error_reporting(NULL);
include 'Connection.php';
include 'Common.php';
include 'Functions/Members.php';
?>
** //错误信息**
<?php
$connection_error = 'Our website is experiencing technical issues, please come back later.';
$wrong_login = 'Password and name are wrong.';
$member_registered = 'Access has been denied. You do not seem to be a registered user.';
?>
** //连接**
<?php
include 'Errors.php';
$connection = mysqli_connect('localhost', 'root', '', 'project') or exit ($connection_error);
?>
** //常见**
<?php
error_reporting();
function sanitize($connection, $data) {
global $connection;
global $data;
return mysqli_real_escape_string($connection, $data);
}
?>
** //成员**
<?php
function member_registered($connection, $name) {
$name = sanitize($connection, $name);
$query = mysqli_query($connection, "SELECT COUNT(`id`) FROM `members` WHERE `name` = '$name'");
return (mysqli_num_rows($query) == 1) ? true : false;
}
?>
** //登录**
<?php
include 'Php/Core/Initializer.php';
if (member_registered($connection, 'ee')) {
echo "exists";
}
die("eee");
echo error_reporting();
if (empty($_POST) == false) {
$name = $_POST['name'];
$password = $_POST['name'];
if (empty($name) OR empty($password)) {
echo $wrong_login;
}
else if (member_registered($connection, $name) == false) {
echo $member_registered;
}
}
?>
更新:
现在我得到了:existsseee;
即使条件设置正确,条件也不起作用。用户名不存在,因此它应该回显&#34;存在:
if (member_registered($connection, 'ee')) {
echo "exists";
}
die("eee");
答案 0 :(得分:0)
在您的情况下,您的清理功能中的executeCommand
变量无法访问。
尝试将$connection
变量传递给您的函数,如下所示:
$connection
在此处详细了解:http://php.net/manual/en/language.variables.scope.php
<强>更新强>
在** // MEMBERS **中,您还需要包含新参数:
function sanitize($data, $connection) {
return mysqli_real_escape_string($connection, $data);
}
因此你的** // LOGIN **也应该更新:
function member_registered($name, $connection) {
$name = sanitize($name, $connection);
$query = mysqli_query($connection, "SELECT COUNT(`id`) FROM `members` WHERE `name` = '$name'");
return (mysqli_num_rows($query) == 1) ? true : false;
}
确保include 'Php/Core/Initializer.php';
if (member_registered('ee', $connection)) {
echo "exists";
}
die("eee");
echo error_reporting();
if (empty($_POST) == false) {
$name = $_POST['name'];
$password = $_POST['name'];
if (empty($name) OR empty($password)) {
echo $wrong_login;
}
else if (member_registered($name, $connection) == false) {
echo $member_registered;
}
}
变量可以在任何需要的地方到达。
更新#2
回答您的更新:
它应该是这样的:
$connection
而不是这样:
if (member_registered('ee', $connection)) {
echo "exists";
}
if (member_registered($connection, 'ee')) {
echo "exists";
}
是第二个参数。