【问题标题】:Undefined variable: mysqli , Fatal error: Call to a member function escape_string() on null [duplicate]未定义的变量:mysqli,致命错误:在 null 上调用成员函数 escape_string() [重复]
【发布时间】:2017-05-05 23:24:11
【问题描述】:

我想为我的网站制作登录和注册表单。每当我点击登录页面时,似乎我有一个错误:

Undefined variable: mysqli and Fatal error: Call to a member function escape_string() on null 

这是我的登录页面编码:

<?php
/* User login process, checks if user exists and password is correct */

// Escape email to protect against SQL injections

$email = $mysqli->escape_string($_POST['email']);
$result = $mysqli->query("SELECT * FROM login WHERE email='$email'");

if ( $result->num_rows == 0 ){ // User doesn't exist
$_SESSION['message'] = "User with that email doesn't exist!";
header("location: error.php");
}
else { // User exists
$user = $result->fetch_assoc();

if ( password_verify($_POST['password'], $user['password']) ) {

    $_SESSION['email'] = $user['email'];
    $_SESSION['first_name'] = $user['first_name'];
    $_SESSION['last_name'] = $user['last_name'];
    $_SESSION['active'] = $user['active'];

    // This is how we'll know the user is logged in
    $_SESSION['logged_in'] = true;

    header("location: profile.php");
}
else {
    $_SESSION['message'] = "You have entered wrong password, try again!";
    header("location: error.php");
}
 }

出现错误:

$email = $mysqli->escape_string($_POST['email']);

需要一些帮助来发现这个错误。

【问题讨论】:

  • 那么你在哪里定义$mysqli

标签: php mysqli login


【解决方案1】:

问题在于 $mysqli 而不是 escape_string,因为 $mysqli 未定义
顺便说一句,这里是声明 $mysqli 的一种方式

$mysqli = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多