2017-05-05 50 views
-2

我想爲我的網站創建登錄和註冊表單。每當我點擊登錄頁面上,似乎我有一個錯誤:未定義變量:mysqli,致命錯誤:調用成員函數escape_string()null

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']); 

極品一些幫助發現這個錯誤。

+1

所以,你在哪裏定義'$ mysqli'? –

回答

0

問題是$ 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(); 
    } 
+0

謝謝。它的作品 –

+0

歡迎@shafiqamarorang –

相關問題