简体   繁体   中英

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.

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.

Also, if your table is InnoDB , make sure that you have autocommit on or issue an explicit commit after the update is made.

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? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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