php - SQL 错误:1064 您的 SQL 语法有错误
问题描述
我有一个名为 users 的表:
____ ____ __________ _____________
| | | | | | | |
| id | | name| |firstCon | | secondCon |
|____| |_____| |________ | |___________|
1 john true false
2 mark false false
我想用or更改firstCon
andsecondCon
值。true
false
所以我使用以下查询:
$sql = "UPDATE users SET ? = ? WHERE name = ?";
$query->bind_param($condition, $value, $name);
其中or , = ,$condition
是用户名。firstCon
secondCon
$value
true/false
$name
我得到那个错误:
1064 You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version
for the right syntax to use near '? = ? WHERE name = ?'
我正在使用该方法,因为我不知道选择了哪个条件,所以我取决于名称。
解决方案
您不能将列名(或其他标识符)作为参数传递。这是另一种方法,不需要修改查询字符串:
UPDATE users
SET firstcon = (case when ? = 'firstcon' then ? else firstcon end),
secondcon = (case when ? = 'secondcon' then ? else secondcon end)
WHERE name = ?;
注意:这有更多的占位符。如果将参数作为命名参数传递可能会更简单:
UPDATE users
SET firstcon = (case when :which = 'firstcon' then :value else firstcon end),
secondcon = (case when :which = 'secondcon' then :value else secondcon end)
WHERE name = :name;
推荐阅读
- php - 如何将文件保存到由 php 输入 type="text" 给出的目录中?
- php - CakePHP:从 3.6.x 升级到 4.0“缺少模板异常”
- security - 如何确定公钥的真实性
- android - 您将如何在 Firestore 上构建互评社交网络模型?
- c - 如何释放()由malloc()分配的结构数组?
- kotlin - 什么时候是详尽的,所以其他是多余的?
- java - 如何为没有域的主机创建 SSL 证书?
- gstreamer - 音频流:使用 Gstreamer 接收 RTP-Stream - 延迟
- python - Python 3:从 tar.gz 存档中提取文件
- jetty - Apache Karaf 中无法安装 Keycloak 码头适配器