PHP PDO相关:更新SQL语句未更新数据库内容
我试图在 PHP 脚本中使用准备好的语句来实现更新语句,但它似乎它没有更新数据库中的记录,我不确定为什么,所以如果您能分享一些见解,我将不胜感激。
代码
$query = "UPDATE DatTable SET DF_PARTY_ID = :party_id,
DF_PARTY_CODE = :party_code,
DF_CONNECTION_ID = :connection_id WHERE DF_PARTY_ID = ':party_id'";
$stmt = $this->connection->prepare($query);
$stmt->bindValue(':party_id', $data[0], PDO::PARAM_INT);
$stmt->bindValue(':party_code', $data[1], PDO::PARAM_INT);
$stmt->bindValue(':connection_id', $data[2], PDO::PARAM_INT);
$stmt->execute();
鼓舞人心导致这种方法的解决方案。我该如何解决这个问题?
I am trying to implement an update statement using a prepared statement in a PHP script, but it appears that it is not updating the record in the database and I am not sure why and so would appreciate if you can share some insights.
Code
$query = "UPDATE DatTable SET DF_PARTY_ID = :party_id,
DF_PARTY_CODE = :party_code,
DF_CONNECTION_ID = :connection_id WHERE DF_PARTY_ID = ':party_id'";
$stmt = $this->connection->prepare($query);
$stmt->bindValue(':party_id', $data[0], PDO::PARAM_INT);
$stmt->bindValue(':party_code', $data[1], PDO::PARAM_INT);
$stmt->bindValue(':connection_id', $data[2], PDO::PARAM_INT);
$stmt->execute();
Inspiring solution leading to this approach. How can I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能没有帮助,但是为什么你只绑定 3 个变量,而有 4 个变量呢?我不能说我有在 PHP 中执行此操作的经验,但在 Perl 和 Oracle 中它会抛出错误。我会尝试绑定 2 个 SET 和 1 个 WHERE,然后删除第一个分配,看看是否有效。
Might not help, but why are you only binding 3 variables, when there are 4? I can't say that I have experience doing this in PHP, but in Perl and Oracle it would throw an error. I'd try binding the 2 SETs and the 1 WHERE, and removing the first assignment, and see if that works.
确保您尝试更新的
party_id
存在于数据库中。另外,如果您的表是
InnoDB
,请确保您已启用autocommit
或在更新后发出显式提交。Make sure that the
party_id
you are trying to update exists in the database.Also, if your table is
InnoDB
, make sure that you haveautocommit
on or issue an explicit commit after the update is made.我不确定你是否想做你想做的事。
您的 UPDATE 语句基本上表示根据 NEW 值更新键和两个值,因为 party_id 位于 SET 和 WHERE 子句中。
您可能需要将准备好的语句更改为:
UPDATE DatTable SET DF_PARTY_ID = :party_id,
DF_PARTY_CODE = :派对代码,
DF_CONNECTION_ID = :connection_id
WHERE DF_PARTY_ID = ':old_party_id'
将新的 party_id 值绑定到 :party_id 并将当前值绑定到 :old_party_id
I'm not sure if you want to do what you're trying to do.
Your UPDATE statement basically says update the key and two values based on the NEW value, since party_id is in the SET and WHERE clauses.
You may want to change your prepared statement to this:
UPDATE DatTable SET DF_PARTY_ID = :party_id,
DF_PARTY_CODE = :party_code,
DF_CONNECTION_ID = :connection_id
WHERE DF_PARTY_ID = ':old_party_id'
bind your NEW party_id value to :party_id and the CURRENT one to :old_party_id
必须实现基本的错误处理,而不是猜测:
Instead of guessing, basic error handling must be implemented: