2014-09-27 48 views
2

我正在做PHP編碼的第一次。我得到了以下錯誤:在PHP代碼中的錯誤

錯誤:

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 9

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 10

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 11

Notice: Undefined index: empID in C:\xampp\htdocs\331002.php on line 12

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 12

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 17

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 18

這裏是我的代碼:

<?php 
$con=mysqli_connect("localhost","root","root","student"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// escape variables for security 
$FName = mysqli_real_escape_string($con, $_POST['firstName']); 
$LName = mysqli_real_escape_string($con, $_POST['lastName']); 
$Salary = mysqli_real_escape_string($con, $_POST['salary']); 
$ID = mysqli_real_escape_string($con, $_POST['empID']); 

$sql="INSERT INTO PersonInfo (FName, LName, empID, Salary) 
VALUES ('$FName', '$LName','$ID','$Salary')"; 

if (!mysqli_query($con,$sql)) { 
    die('Error: ' . mysqli_error($con)); 
} 
echo "1 record added"; 

mysqli_close($con); 
?> 
+1

我猜mysqli_connect()返回錯誤狀態(這是一個布爾值,而不是一個mysqli對象)。您可以通過執行'$ con = mysqli_connect(「localhost」,「root」,「root」,「student」)或者die(「Connection error:」。mysqli_error($ con))來輕鬆測試;' – Oliver 2014-09-27 17:58:24

+0

您有錯誤建立mysqli對象($ con) – Ormoz 2014-09-27 18:00:57

回答

3

通常的 「檢查連接」 模式是這樣的:

<?php 
$con=mysqli_connect("localhost","root","root","student"); 
// Check connection 
if (false === $con) { 
    // die will "finish" the script 
    die("Failed to connect to MySQL: " . mysqli_connect_error()); 
} 

mysqli_real_escape_string需要有效的連接,您似乎沒有。

1

該消息明確告訴您mysqli_real_escape_string的第一個參數是布爾型的數據類型。如果您不知道數據類型之間的差異,我建議您查看它們,瞭解它們對於使用任何語言進行編程都非常重要。如果你檢查文檔,你會發現它需要「mysqli_connect()或mysqli_init()返回的鏈接標識符」。

你對mysqli_connect()的調用返回false,你想弄清楚爲什麼,並且當它返回false時處理大小寫。猜猜有一個連接錯誤。

甚至有一個展示這個PHP手冊中的頁面上例如:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
+0

Thanx你們所有人..那工作.. :) – MukulAgr 2014-09-27 18:12:55

1

你的問題就出在你的腳本的頂部:

$con=mysqli_connect("localhost","root","root","student"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// escape variables for security 
$FName = mysqli_real_escape_string($con, $_POST['firstName']); 

您已檢查了的mysqli連接,但是如果出現連接錯誤,您的腳本就會迴應並繼續。如果存在連接錯誤,該腳本應該die,或至少跳過DB相互作用,因爲它不能與數據庫事務繼續:

$con = mysqli_connect("example.com", "user", "password", "database"); 
if (mysqli_connect_errno($con)) { 
    die("Failed to connect to MySQL: " . mysqli_connect_error()); 
}