douhuai2861 2017-05-05 23:24
浏览 52
已采纳

未定义的变量:mysqli,致命错误:调用null [duplicate]上的成员函数escape_string()

I want to make login and register form for my web. Whenever I click on login page, seems that I have an error :

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

and here is my coding for login page:

<?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");
}
 }

The error was on:

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

Need some help to discover this error.

</div>
  • 写回答

1条回答 默认 最新

  • duanchanguo7603 2017-05-05 23:32
    关注

    the problem is with $mysqli not with escape_string because $mysqli is not defined
    by the way here is a way de declare $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();
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条