gpt4 book ai didi

php - 如何为更新查询准备语句?

转载 作者:IT老高 更新时间:2023-10-28 23:44:53 27 4
gpt4 key购买 nike

我有一个带有以下代码的 mysqli 查询:

$db_usag->query("UPDATE Applicant SET phone_number ='$phone_number', 
street_name='$street_name', city='$city', county='$county', zip_code='$zip_code', day_date='$day_date', month_date='$month_date',
year_date='$year_date' WHERE account_id='$account_id'");

但是,所有数据都是从 HTML 文档中提取的,因此为了避免错误,我想使用准备好的语句。我找到了 PHP documentation on bind_param()但没有 UPDATE 示例。

最佳答案

UPDATE 的工作方式与插入或选择相同。只需用 ? 替换所有变量。

$sql = "UPDATE Applicant SET phone_number=?, street_name=?, city=?, county=?, zip_code=?, day_date=?, month_date=?, year_date=? WHERE account_id=?";

$stmt = $db_usag->prepare($sql);

// This assumes the date and account_id parameters are integers `d` and the rest are strings `s`
// So that's 5 consecutive string params and then 4 integer params

$stmt->bind_param('sssssdddd', $phone_number, $street_name, $city, $county, $zip_code, $day_date, $month_date, $year_date, $account_id);
$stmt->execute();

if ($stmt->error) {
echo "FAILURE!!! " . $stmt->error;
}
else echo "Updated {$stmt->affected_rows} rows";

$stmt->close();

关于php - 如何为更新查询准备语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514649/

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