昨天我有一个关于“pdo插入和更新”的主题。现在是以下内容
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in on line 148
这是我的代码。
$sth = $db->prepare(
'INSERT INTO track (`rsname`, `overallranknow`, `overalllevelnow`, `overallxpnow` )' .
'VALUES (:name, :Overalln, :Overall1, :Overall2) '.
'ON DUPLICATE KEY UPDATE ' .
"rsname = :name" .
"overallranknow = :Overalln" .
"overalllevelnow = :Overall1" .
"overallxpnow = :Overall2"
);
$sth->bindValue(':name', $name, PDO::PARAM_STR);
$sth->bindValue(':Overalln', $Overalln, PDO::PARAM_INT);
$sth->bindValue(':Overall1', $Overall[1], PDO::PARAM_INT);
$sth->bindValue(':Overall2', $Overall[2], PDO::PARAM_INT);
$sth->execute();
错误说我的执行行是错误的1。我已经用数组尝试过,但仍然得到了它。我希望也许知道我的意思。
~Kev(英语不好=抱歉)
最佳答案
You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement.
这(遗憾的是)意味着您必须使用辅助名称复制绑定(bind),才能在查询中两次使用相同的值。不漂亮,比如;
$sth = $db->prepare(
'INSERT INTO track (`rsname`, `overallranknow`, `overalllevelnow`, `overallxpnow` )' .
'VALUES (:name, :Overalln, :Overall1, :Overall2) '.
'ON DUPLICATE KEY UPDATE ' .
"rsname = :name_2" .
"overallranknow = :Overalln_2" .
"overalllevelnow = :Overall1_2" .
"overallxpnow = :Overall2_2"
);
$sth->bindValue(':name', $name, PDO::PARAM_STR);
$sth->bindValue(':name_2', $name, PDO::PARAM_STR);
$sth->bindValue(':Overalln', $Overalln, PDO::PARAM_INT);
$sth->bindValue(':Overalln_2', $Overalln, PDO::PARAM_INT);
$sth->bindValue(':Overall1', $Overall[1], PDO::PARAM_INT);
$sth->bindValue(':Overall1_2', $Overall[1], PDO::PARAM_INT);
$sth->bindValue(':Overall2', $Overall[2], PDO::PARAM_INT);
$sth->bindValue(':Overall2_2', $Overall[2], PDO::PARAM_INT);
$sth->execute();
编辑:由于 MySQL 允许 VALUES
ON DUPLICATE KEY
中的关键字不必重复参数,您最好使用 @YourCommonSense 针对这种情况的答案。
关于mysql - pdo执行给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564257/