我有很多函数需要更改为MYSQLI。
有人能帮忙写正确的密码吗?修复此问题将允许更改此问题的其余功能。非常感谢。
错误以**开头,以**结尾,并跟随问题行。
connection.php
<?php
mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
?>
常规.php
<?php
function sanitize($data) {
mysqli_real_escape_string($connect,$data);
}
function protect_page() {
if (logged_in() === false) {
header('Location:../../redirect.php');
}
}
function logged_in_redirect() {
if (logged_in() === true) {
header('Location: ../../dashboard.php');
exit();
}
}
?>
users.php
<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysqli_query($connect,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
mysqli_num_rows($query), 0) == 1) ? true : false;
}
function logged_in() {
return (isset($_SESSION['id'])) ? true : false;
}
?>
mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
?>
php…这是我试图回显的地方,如果用户存在,应该返回用户"偷来的",但事实并非如此。顺便说一句,形式很好,已经检查过了。
<?php
include 'core/init.php';
if (user_exists('stole') === false) {
echo 'exists';
}
die ();
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) === true || empty($password) === true) {
$errors [] = 'Please enter a valid username and password';
} else if (user_exists($username) === false) {
$errors [] = 'We can''t find that username. Have you registered? ';
}
}
?>
init.php
您的脚本中存在一些问题:
-
$connect_error
未定义。(由@Fred ii-相关) -
您正在使用会话
$_SESSION['id']
,没有指示正在使用session_start();
。它在所有使用会话的文件中都是必需的。(由@Fred ii-相关) -
您没有将资源添加到全局变量中,您创建的方法将永远无法访问
mysqli_connect('localhost', 'root', '', 'info')
。
了解变量范围:
http://php.net/manual/en/language.variables.scope.php
了解会话:
- http://php.net/manual/en/features.sessions.php
- http://php.net/manual/en/book.session.php
试试这个:
<?php
$connect = mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
?>
常规.php
<?php
function sanitize($data) {
global $connect;
mysqli_real_escape_string($connect,$data);
}
function protect_page() {
if (logged_in() === false) {
header('Location:../../redirect.php');
}
}
function logged_in_redirect() {
if (logged_in() === true) {
header('Location: ../../dashboard.php');
exit();
}
}
?>
users.php
<?php
$connect = mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
function user_exists($username) {
global $connect;
$username = sanitize($username);
$query = mysqli_query($connect,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
mysqli_num_rows($query), 0) == 1) ? true : false;
}
function logged_in() {
return (isset($_SESSION['id'])) ? true : false;
}
?>