【发布时间】:2019-04-06 10:41:44
【问题描述】:
尝试使用 php PDO 更新语句更新我的数据库表,该语句通过现有的 id 更新表。
if (isset($_POST["save"])) {
$id = $_POST["save"];
$my_editor = $_POST["my_editor"];
$sql = 'UPDATE advert_composer SET content = :content WHERE id = 1';
$stmt = $connection->prepare($sql);
$stmt->execute(['my_editor' => $my_editor, 'id' => $id ]);
if ($stmt->rowCount()) {
$_SESSION['msg'] = ['type' => 'success', 'message' => 'Content created successfully'];
} else {
$_SESSION['msg'] = ['type' => 'info', 'message' => 'Error Occured Creating Content Try again later'];
}
}
这是我遇到的错误。
致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效参数 number:绑定变量的数量与中的标记数量不匹配 /opt/lampp/htdocs/ubth_recruit/admin/advert_composer.php:16 堆栈 跟踪:#0 /opt/lampp/htdocs/ubth_recruit/admin/advert_composer.php(16): PDOStatement->execute(Array) #1 {main} 抛出 /opt/lampp/htdocs/ubth_recruit/admin/advert_composer.php 在第 16 行
【问题讨论】:
-
my_editor!=content- 您在查询中绑定:content,但尝试执行绑定到my_editor。然后,您的 ID 是静态的(在查询中分配给 1)并且您尝试在执行中绑定它。 -
当您在查询中已将 ID 设置为 1 时,您正在尝试绑定 ID