gpt4 book ai didi

php - PHP代码中的错误

转载 作者:行者123 更新时间:2023-11-29 01:28:45 24 4
gpt4 key购买 nike

我是第一次编写 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);
?>

最佳答案

通常的“检查连接”模式如下:

<?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 需要一个有效的连接,而您似乎没有。

关于php - PHP代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26077515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com