gpt4 book ai didi

php - "No data supplied for parameters in prepared statement"

转载 作者:可可西里 更新时间:2023-11-01 07:56:24 28 4
gpt4 key购买 nike

所以我正在重新编写一个脚本以包含准备好的语句。之前它工作正常,但现在我在脚本运行时收到“没有为准备好的语句中的参数提供数据”。这里有什么问题?

<?php
require_once("models/config.php");


$firstname = htmlspecialchars(trim($_POST['firstname']));
$firstname = mysqli_real_escape_string($mysqli, $firstname);
$surname = htmlspecialchars(trim($_POST['surname']));
$surname = mysqli_real_escape_string($mysqli, $surname);
$address = htmlspecialchars(trim($_POST['address']));
$address = mysqli_real_escape_string($mysqli, $address);
$gender = htmlspecialchars(trim($_POST['gender']));
$gender = mysqli_real_escape_string($mysqli, $gender);
$city = htmlspecialchars(trim($_POST['city']));
$city = mysqli_real_escape_string($mysqli, $city);
$province = htmlspecialchars(trim($_POST['province']));
$province = mysqli_real_escape_string($mysqli, $province);
$phone = htmlspecialchars(trim($_POST['phone']));
$phone = mysqli_real_escape_string($mysqli, $phone);
$secondphone = htmlspecialchars(trim($_POST['secondphone']));
$secondphone = mysqli_real_escape_string($mysqli, $secondphone);
$postalcode = htmlspecialchars(trim($_POST['postalcode']));
$postalcode = mysqli_real_escape_string($mysqli, $postalcode);
$email = htmlspecialchars(trim($_POST['email']));
$email = mysqli_real_escape_string($mysqli, $email);
$organization = htmlspecialchars(trim($_POST['organization']));
$organization = mysqli_real_escape_string($mysqli, $organization);
$inriding = htmlspecialchars(trim($_POST['inriding']));
$inriding = mysqli_real_escape_string($mysqli, $inriding);
$ethnicity = htmlspecialchars(trim($_POST['ethnicity']));
$ethnicity = mysqli_real_escape_string($mysqli, $ethnicity);
$senior = htmlspecialchars(trim($_POST['senior']));
$senior = mysqli_real_escape_string($mysqli, $senior);
$student = htmlspecialchars(trim($_POST['student']));
$student = mysqli_real_escape_string($mysqli, $student);


$order= "INSERT INTO persons (firstname, surname, address, gender, city, province, postalcode, phone, secondphone, email, organization, inriding, ethnicity, senior, student_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($mysqli, $order);
mysqli_stmt_bind_param($stmt, "sssd", $firstname, $surname, $address, $gender, $city, $province, $postalcode, $phone, $secondphone, $email, $organization, $inriding, $ethnicity, $senior, $student);
mysqli_stmt_execute($stmt);
echo $stmt->error;

$result = mysqli_query($mysqli,$stmt);
if ($result === false) {
echo "Error entering data! <BR>";
echo mysqli_error($mysqli);
} else {
echo "User $firstname added <BR>";
}
?>

提前致谢。

最佳答案

您只通过控制字符串“sssd”绑定(bind)了四个参数,但您有很多参数。 mysqli绑定(bind)变量时,每个参数需要一个字符,例如:

mysqli_stmt_bind_param($stmt, "sssdsssssssssdd", $firstname, $surname, $address, 
$gender, $city, $province, $postalcode, $phone, $secondphone, $email,
$organization, $inriding, $ethnicity, $senior, $student);

(我假设 senior 和 student 是整数,并且需要“d”代码。)

您不需要使用 mysqli_real_escape_string() 处理任何变量——这就是使用参数的意义所在。如果您也转义,您将在数据库中的数据中获得文字反斜杠字符。

并且在任何情况下都不需要使用 htmlspecialchars() - 在输出到 HTML 时会使用它,而不是在插入到数据库时使用它。您将在数据库中的数据中获得像 & 这样的文字序列。


关于你的下一个错误:

"Catchable fatal error: Object of class mysqli_stmt could not be converted to string in..."

这是由以下原因造成的:

$result = mysqli_query($mysqli,$stmt);

该函数期望第二个参数是一个字符串,一个新的 SQL 查询。但是您已经准备好该查询,因此您需要以下内容:

$result = mysqli_stmt_execute($stmt);

关于php - "No data supplied for parameters in prepared statement",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514921/

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