我正在做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);
?>
我猜mysqli_connect()返回錯誤狀態(這是一個布爾值,而不是一個mysqli對象)。您可以通過執行'$ con = mysqli_connect(「localhost」,「root」,「root」,「student」)或者die(「Connection error:」。mysqli_error($ con))來輕鬆測試;' – Oliver 2014-09-27 17:58:24
您有錯誤建立mysqli對象($ con) – Ormoz 2014-09-27 18:00:57