我有很多需要更改为MYSQLI的函数。
有人可以帮忙进行适当的重新编码吗?修复此问题将使此问题能够更改其余功能。谢谢。
错误以**开头,以**结束,后面跟着问题行。
connection.php
<?php
mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
?>
general.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);
?>
login.php ...如果用户存在,我试图回应的地方,应该是让用户退回&#39;偷走&#39;但事实并非如此。顺便说一下,表格还不错,已经检查过了。
<?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
答案 0 :(得分:2)
您的脚本中存在一些问题:
$connect_error
未定义。 (由@ Fred-ii相关 - )
您正在使用会话$_SESSION['id']
,但没有使用session_start();
的迹象。在使用会话的所有文件中都需要它。 (由@ Fred-ii相关 - )
您没有将资源添加到全局变量,您创建的方法永远无法访问mysqli_connect('localhost', 'root', '', 'info')
。
http://php.net/manual/en/language.variables.scope.php
试试这个:
<?php
$connect = mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
?>
general.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;
}
?>