【问题标题】:MySQL PDO prepared statement updating columns incorrectlyMySQL PDO 准备语句错误地更新列
【发布时间】:2021-12-25 07:30:51
【问题描述】:

我的 php 中有逻辑可以根据定义的类自动构建更新/插入查询。 它正在生成以下 sql/params: sql:

UPDATE `sessions` SET 
`date`=:date,
`client`=:client,
`rate`=:rate,
`notes`=:notes,
`location`=:location,
`includedimagecount`=:includedimagecount,
`paid`=:paid,
`includedimagesdownloaded`=:includedimagesdownloaded,
`additionalpaid`=:additionalpaid,
`additionalimageprice`=:additionalimageprice,
`readyforclient`=:readyforclient,
`additionalimagesdownloaded`=:additionalimagesdownloaded 
WHERE uid=:_id_

参数:

[date:"2021-11-22 22:34:00",
client:"D3036CCD-D3C1-44B0-B729-B9B7D72769D9",
rate:"85",
notes:"",
location:"nowhere",
includedimagecount:"10",
paid:1,
includedimagesdownloaded:1,
additionalpaid:0,
additionalimageprice:"4",
readyforclient:1,
additionalimagesdownloaded:0,
_id_:"F23E8D6B-2ED7-4F7A-B59B-42CBDD7B4A5B"]

由于某种原因,我无法弄清楚,在此运行之后 - 我的数据库将列 additionalPaidAdditionalImagesDownloaded 显示为 1,即使这些值在更新参数中显然是 0。

如果有帮助,这里是表的架构...

CREATE TABLE IF NOT EXISTS `sessions` (
  `Uid` varchar(36) NOT NULL,
  `Date` datetime DEFAULT NULL,
  `Client` varchar(36) NOT NULL,
  `Rate` float NOT NULL,
  `Notes` text,
  `Location` varchar(256) DEFAULT NULL,
  `IncludedImageCount` smallint NOT NULL DEFAULT '10',
  `AdditionalImagePrice` float NOT NULL DEFAULT '4',
  `Paid` bit(1) DEFAULT b'0',
  `IncludedImagesDownloaded` bit(1) DEFAULT b'0',
  `AdditionalPaid` bit(1) DEFAULT b'0',
  `ReadyForClient` bit(1) DEFAULT b'0',
  `AdditionalImagesDownloaded` bit(1) DEFAULT b'0',
  PRIMARY KEY (`Uid`),
  UNIQUE KEY `sessions_uid_uindex` (`Uid`),
  KEY `sessions_client_fk` (`Client`)

当我直接在 datagrip 中运行相同的查询(仍然使用值占位符)时,它按预期工作。

编辑

我可以通过发送一个较小的对象来解决这个问题,only 是我真正想要更新的列(和 id)。但这是一个开源框架,我需要确保此功能按预期工作。

编辑 2

Here is the code 有问题。 传入的对象是一个类的实例,在属性/表列上具有 1 对 1 匹配。

编辑 3

我也尝试在参数数组中使用:(以匹配查询),并得到相同的结果。

【问题讨论】:

  • 我们可能需要查看 PHP 代码来回答您的问题。
  • 当然,现在添加问题的链接!

标签: php mysql pdo prepared-statement


【解决方案1】:

尝试将它们的数据类型从 bit 更改为 tinyint(1)。有时我通常会失败。

【讨论】:

  • 我已经这样做了,只是为了让它工作。在这个特定的项目中,我并没有过分关注它,但我拥有的基本代码 - 我正在编写一个公共框架。我只是想确保它有效且可靠。如果有什么我可以做代码端来修复我想要的位。 - 注意,我还没有尝试切换到bind 语法而不是参数数组。
  • 好吧,tinyint(1) 也可以像 bit 一样工作,传递 0 或 1 也会被 tinyint(1) 读取为真或假
猜你喜欢
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 2018-01-14
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多