简体   繁体   English

与PHP PDO相关:更新SQL语句不更新数据库内容

[英]PHP PDO related: Update SQL statement not updating the content of the database

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. 我正在尝试使用PHP脚本中的准备好的语句来实现更新语句,但是看来它没有更新数据库中的记录,并且我不确定为什么,因此如果您能分享一些见解,将不胜感激。

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? 我该如何解决这个问题?

Make sure that the party_id you are trying to update exists in the database. 确保数据库中存在您要更新的party_id

Also, if your table is InnoDB , make sure that you have autocommit on or issue an explicit commit after the update is made. 另外,如果您的表是InnoDB ,请确保在进行更新后已autocommit或发出显式提交。

Instead of guessing, basic error handling must be implemented: 不能猜测,必须实现基本的错误处理:

$arr = $stmt->errorInfo();
print_r($arr);

Might not help, but why are you only binding 3 variables, when there are 4? 可能无济于事,但是为什么只有4个绑定三个变量呢? I can't say that I have experience doing this in PHP, but in Perl and Oracle it would throw an error. 我不能说我有在PHP中执行此操作的经验,但是在Perl和Oracle中会抛出错误。 I'd try binding the 2 SETs and the 1 WHERE, and removing the first assignment, and see if that works. 我会尝试将2个SET和1个WHERE绑定在一起,然后删除第一个分配,看看是否可行。

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. 您的UPDATE语句基本上说根据NEW值更新密钥和两个值,因为party_id在SET和WHERE子句中。

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' 更新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 将您的新party_id值绑定到:party_id,将当前值绑定到:old_party_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM