您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

PHP UPDATE准备语句

PHP UPDATE准备语句

$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语句中仅设置一列。其他列将不会更改。

php 2022/1/1 18:21:49 有553人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶