PHP登录系统可在本地运行,但不适用于Hostgator PHP 7.2 [英] PHP login system works on local but doesn't work on Hostgator PHP 7.2
问题描述
PHP登录系统可在本地运行,但不适用于Hostgator PHP 7.1.我的本地是PHP 7.2
PHP login system works on local but doesn't work on Hostgator PHP 7.1. My local is PHP 7.2
我已经在本地计算机上建立了一个可以正常运行的门户.我可以在本地计算机上进行CRUD.一旦我将其在线放置在服务器上,登录系统就无法正常工作.我仍然可以在数据库中填充用户信息时注册新用户,因此这不是数据库配置问题.我收到这些错误:
I've built out a fully working portal on my local machine. I can CRUD on my local machine. As soon as I put it on the server online, the login system doesn't work. I still can register new users as the user info populates in the DB, so its not a DB config issue. I am getting these errors:
警告:mysqli_stmt_bind_param():变量的数量与......中准备好的语句中的参数数量不匹配.
Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in .....
致命错误:未被捕获的错误:在...中调用未定义的函数mysqli_stmt_get_result().
Fatal error: Uncaught Error: Call to undefined function mysqli_stmt_get_result() in ....
我花了5个小时试图弄清为什么它不能在Hostgator服务器上运行,而是可以在我的本地服务器上运行.
I have spent 5 hours trying to figure out why it won't work on the Hostgator server but will work on my local.
这是我的代码:
if(isset($_POST['loginSubmit'])){
//include connection configs
require '../../db_configs.php';
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
if(empty($email || $password )){
header("Location: ../../../portalSignIn.php?signin=fieldsempty");
exit();
}
else
{
//user email query
$sqlEmail = "SELECT * FROM users WHERE email='$email'";
$emailResult = mysqli_query($conn, $sqlEmail);
$stmt = mysqli_stmt_init($conn);
//SQL Error
if(!mysqli_stmt_prepare($stmt, $sqlEmail)){
header("Location: ../../../portalSignIn.php?signin=SQL_FAILED");
exit();
}
if(mysqli_num_rows($emailResult) == 0){
//email doesn't exist
header("Location: ../../../portalSignIn.php?signin=incorrectemail");
exit();
}
else
{
//bind data if email exists
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($result)){....
这时断了-> mysqli_stmt_bind_param($stmt, "s", $email);
我研究了 https://www.plus2net.com/php_tutorial/mysqli_mysqlnd. php 和Hostagtor没有这些设置.而且我在注册页面上成功使用了mysqli_stmt_bind_param()
.
I've looked into https://www.plus2net.com/php_tutorial/mysqli_mysqlnd.php and Hostagtor doesn't have these settings. And I have used mysqli_stmt_bind_param()
successfully on the sign up page.
推荐答案
您的代码中有很多错误.让我向您解释其中的一些.
You have plenty of mistakes in your code. Let me explain a few of them to you.
- 不要使用
mysqli_real_escape_string()
转义变量.只是根本不使用此功能. -
empty($email || $password )
将检查布尔值.这没有多大意义.删除empty
调用并检查否定.如果$email
和$password
都不为空,则 - 请勿使用
mysqli_query
.您将要准备一条语句,因此请勿调用此函数.另外,您还需要参数化SQL.使用?
作为该值的占位符.
在此位置的 -
mysqli_num_rows
会引发错误.您根本不需要此功能.如果要使用它,则需要将get_result()
的输出作为参数传递
- 要使用prepared语句获取值,您需要准备/绑定/执行/获取结果.只有这样,您才能获取一行(如果存在).如果未返回任何内容,则
$record
将为null.
- Don't escape your variables using
mysqli_real_escape_string()
. Just don't use this function at all. empty($email || $password )
is going to check a boolean value. This does not make much sense. Remove theempty
call and check the negation. If neither$email
nor$password
then one of them is empty.- Don't use
mysqli_query
. You are going to prepare a statement, so do not call this function. Also you need to parameterized the SQL. Use?
as placeholder for the value. mysqli_num_rows
in this place would throw an error. You don't need this function at all. If you wanted to use it you would need to pass the as a parameter the output ofget_result()
- To fetch values using prepared statement you need to prepare/bind/execute/get_result. Only then you can fetch a row if it exists. If nothing is returned then
$record
will be null.
if (isset($_POST['loginSubmit'])) {
//include connection configs
require '../../db_configs.php';
$email = $_POST['email'];
$password = $_POST['password'];
if (!$email || !$password) {
header("Location: ../../../portalSignIn.php?signin=fieldsempty");
exit();
} else {
//user email query
$sqlEmail = "SELECT * FROM users WHERE email=?";
$stmt = $con->prepare($sqlEmail);
$stmt->bind_param('s', $email);
$stmt->execute();
$result = $stmt->get_result();
$record = $result->fetch_assoc();
if (!$record) {
//email doesn't exist
header("Location: ../../../portalSignIn.php?signin=incorrectemail");
exit();
} else {
// handle your data here
}
}
}
和往常一样,不要忘记启用mysqli错误报告.请参见如何在MySQLi中获取错误消息?
And as always don't forget to enable mysqli error reporting. See How to get the error message in MySQLi?
这篇关于PHP登录系统可在本地运行,但不适用于Hostgator PHP 7.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!