php PDOStatement::execute(): SQLSTATE[HY093]: 无效的参数号:未定义参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23134769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
提问by Monil Shah
Below is my code, I am not able to resolve this error. Any help is appreciated. I am trying to update a table in my database.
以下是我的代码,我无法解决此错误。任何帮助表示赞赏。我正在尝试更新数据库中的表。
public function updateUnit($params){
$user = 'monil';
$password = 'Masters123';
$dbh = new \PDO('mysql:host=127.0.0.1;dbname=tcsdb', $user, $password);
$task=array(':UnitCode'=>$params['UnitCode'],':UnitDescription'=>$params['UnitDescription'] ,
':UnitName'=>$params['UnitName'], ':UnitID'=>$params['UnitID']);
echo $params['UnitID'];
$sth = $dbh->prepare('UPDATE unit SET UnitCode = :UnitCode,'
. 'UnitDescription = :UnitDescription,UnitName = :UnitName WHERE UnitId=:UnitId');
$sth->execute($task);
return true;
}
回答by Niet the Dark Absol
:UnitID != :UnitId
:UnitID != :UnitId
Parameters are case-sensitive.
参数区分大小写。
回答by smn
same errors may occur if you use a "." dot in bindParam
如果使用“.”,可能会出现同样的错误。bindParam 中的点
ex.
前任。
$query = "select * from t where t1 = :foo.bar";
$stmt = $pdo->prepare($query);
$stmt->execute([':foo.bar' => 'blah']);
回答by Vaibs
The same error arise when you missed : colon while creating statement.
当您在创建语句时错过 : 冒号时会出现同样的错误。
ex: Below statement throws invalid parameter erroras passwordin VALUES is missing : colon.
例如:下面的语句抛出无效参数错误,因为VALUES 中的密码丢失:冒号。
$stmt = $db->prepare('INSERT INTO members (username,password) VALUES (:username, password)');