$stmt = $this->MysqLi->prepare("UPDATE datadump SET content=? WHERE id=?");
/* BK: always check whether the prepare() succeeded */
if ($stmt === false) {
trigger_error($this->MysqLi->error, E_USER_ERROR);
return;
}
$id = 1;
/* Bind our params */
/* BK: variables must be bound in the same order as the params in your sql.
* Some people prefer PDO because it supports named parameter. */
$stmt->bind_param('si', $content, $id);
/* Set our params */
/* BK: No need to use escaping when using parameters, in fact, you must not,
* because you'll get literal '\' characters in your content. */
$content = $_POST['content'] ?: '';
/* Execute the prepared Statement */
$status = $stmt->execute();
/* BK: always check whether the execute() succeeded */
if ($status === false) {
trigger_error($stmt->error, E_USER_ERROR);
}
printf("%d Row inserted.\n", $stmt->affected_rows);
关于您的问题:
我从脚本中收到一条消息,提示已插入0行
这是因为绑定参数时,它们的顺序颠倒了。因此,您正在id列中搜索$ content的数值,该数值可能解释为0。因此UPDATE的WHERE子句匹配零行。
我需要声明所有字段还是可以只更新一个字段?
可以在UPDATE语句中仅设置一列。其他列将不会更改。