我有一张桌子:
CREATE TABLE `transportCompanies` (
`transportСompanyId` int(11) NOT NULL AUTO_INCREMENT,
`fullName` varchar(200) NOT NULL,
PRIMARY KEY (`transportСompanyId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
和代码:
// this post data just for test
$_POST['data'] = '{ "transportСompanyId": 1,
"fullName": "USER INPUT HERE" }';
$insertData['array'] = json_decode($_POST['data'], true);
$insertData['fields'] = "";
$insertData['placeholders'] = "";
$insertData['keyValues'] = "";
foreach ($insertData['array'] as $column => $data) {
$insertData['fields'] .= "`{$column}`, ";
$insertData['placeholders'] .= ":{$column}, ";
$insertData['keyValues'] .= "`{$column}` = VALUES(`{$column}`), ";
}
$insertData['fields'] = rtrim($insertData['fields'], ", ");
$insertData['placeholders'] = rtrim($insertData['placeholders'], ", ");
$insertData['keyValues'] = rtrim($insertData['keyValues'], ", ");
之后我们有变量:
$insertData['fields']string(33) "`transportСompanyId`, `fullName`"
$insertData['placeholders']string(31) ":transportСompanyId, :fullName"
$insertData['keyValues']string(86) "`transportСompanyId` = VALUES(`transportСompanyId`), `fullName` = VALUES(`fullName`)"
但在执行sql-query之后:
$sql = "INSERT INTO `{$tableName}` ({$insertData['fields']})
VALUES ({$insertData['placeholders']})
ON DUPLICATE KEY UPDATE
{$insertData['keyValues']}";
$stmt = $dbh->prepare($sql);
$stmt->execute($insertData['array']);
我收到错误:
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
这是$stmt->debugDumpParams()
输出:
SQL: [232] INSERT INTO `transportCompanies` (`transportСompanyId`, `fullName`)
VALUES (:transportСompanyId, :fullName)
ON DUPLICATE KEY UPDATE
`transportСompanyId` = VALUES(`transportСompanyId`), `fullName` = VALUES(`fullName`)
Params: 2
Key: Name: [20] :transportСompanyId
paramno=-1
name=[20] ":transportСompanyId"
is_param=1
param_type=2
Key: Name: [9] :fullName
paramno=-1
name=[9] ":fullName"
is_param=1
param_type=2
我不知道该怎么想,因为查询中有两个参数,我在数组中传递了两个参数。没有拼写错误。如果我没有通过transportCompanyId
,那么请求会正常通过,但它没有意义,因为如果表中已经存在transportCompanyId
,我需要更新该行。
我想我错过了什么,请帮忙。谢谢。
修改
好的,更简单的代码版本也不起作用。 SQL注入绝对为零,但错误相同。
$sql = "INSERT INTO `transportCompanies` (`transportСompanyId`, `fullName`)
VALUES (:transportСompanyId, :fullName)
ON DUPLICATE KEY UPDATE
`transportСompanyId` = VALUES(`transportСompanyId`), `fullName` = VALUES(`fullName`)";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(":transportСompanyId", 1);
$stmt->bindValue(":fullName", "TEST");
$stmt->execute();
答案 0 :(得分:0)
好的,我明白了。
出于某种原因,php无法使用transportСompId
名称绑定参数。与名为transportСId
,tСI
,transСompId
,transportId
的参数相同的问题。
我无法准确理解错误的原因,但是如果我将列重命名为<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pause_active" android:state_enabled="false" />
<item android:drawable="@drawable/play_active" android:state_checked="true" />
</selector>
,则所有操作都完美无缺。也许有人知道对参数名称的具体限制是什么?