php mysql bind-param,如何为更新查询准备语句

Mic*_*el 26 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'");
Run Code Online (Sandbox Code Playgroud)

但是,所有数据都是从HTML文档中提取的,所以为了避免错误,我想使用预准备语句.我找到了PHP文档,但没有UPDATE示例

mysqli stmt bind param

Mic*_*ski 62

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();
Run Code Online (Sandbox Code Playgroud)

  • @ArabMichael:让您的数据库使用UTF8编码,将PHP的内部编码设置为UTF8([iconv_set_encoding](http://php.net/manual/en/function.iconv-set-encoding.php))你应该没问题. (2认同)