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

标签 php mysql mysqli prepared-statement bindparam

我有一个带有以下代码的 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/

相关文章:

php - 在 PHP 中响应来自客户端站点的 Https POST 请求

PHP 脚本在浏览器中以交互方式取消链接文件,但不是作为 cPanel cron 作业

php - 从数据库中删除文件或照片时如何从文件夹中删除它?

java - 如果在尝试将对象保存在数据库中时由 hibernate 生成 SQL 语法错误,如何解决?

php - 有没有一种方法可以通过 PHP 删除注释而无需为其分配 ID?

php - 将 mysql 函数转换为 mysqli 准备好的语句并保持相同的输出

php - 显示 php mysql 大段落的前 3 行

mysql - SQL 从 COL1 获取 AVG,其中 COL2 = 0

java - 为什么在我的查询中添加 Where 子句时没有得到结果?

php - MySQL php get column using another column(使用用户名获取密码)