阅读之前:我知道表格太奇怪太长,需要标准化;但由于某种原因,这是我的数据库,我应该使用这个表!对于那个很抱歉! 为了保持简短: 我遇到以下异常:
exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\xampp\htdocs\FD\includes\helper.php:472 Stack trace:
0 C:\xampp\htdocs\FD\includes\helper.php(472): PDOStatement->execute(Array) #1
C:\xampp\htdocs\FD\newHrForm.php(113): helperFunctions::UpdateTableHrForms('112', 'dfsfdsfds', '3123213', 'dfdsf', '', '', '', '', '', '', '', '', '', '', '', '3213123213', '1', '3213123', '213123213', '3213213', '213123123', ) #2 {main}
我保存数据的函数如下:
public static function UpdateTableHrForms(
$id,$dob_city,$dob_province,$dob_country,...)
{
$conn = new mysqlcon();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT into `hr_forms` (`id`,`dob_city`,
`dob_province`,`dob_country`,...)
VALUES (:id,:dob_city,:dob_province,:dob_country,...)";
try {
if (helperFunctions::CheckidExistForms($id) == 0) {
$result = $conn->prepare($query);
$result->execute(array('id'=>$id,'dob_city'=>$dob_city,'dob_province'=>$dob_province,'dob_country'=>$dob_country));
$Msg = "<div style=\"text-align:center;\" class=\"alert alert-success\">
<strong>Tips! </strong>
Data is successfully saved to database.
<button class=\"close\" data-dismiss=\"alert\" type=\"button\">×</button>
</div>";
} else {
$Msg = "<div style=\"text-align:center;\" class=\"alert alert-error\">
<strong>Error! </strong>
This employee information is already existed in the system.
<button class=\"close\" data-dismiss=\"alert\" type=\"button\">×</button>
</div>";
}
} catch (Exception $e) {
$e->getMessage();
$Msg = $e; /* "<div style=\"text-align:center;\" class=\"alert alert-error\">
<strong>Error! </strong>
This employee information cannot save in the system.
<button class=\"close\" data-dismiss=\"alert\" type=\"button\">×</button>
</div>"; */
}
return $Msg;
}
当我替换插入方法的值时(在 UpdateTableHrForms 中使用错误给我的值并在 mysql 中运行它;没有错误;但在 PHP 中它给了我错误;如果我我做错了什么吗?)
最佳答案
您传递给execute
的数组格式不正确;您需要在参数名称的开头添加冒号。
应该是:
array(':id'=>$id,':dob_city'=>....
关于php - 异常 'PDOException',消息为“SQLSTATE[HY093] : Invalid parameter number: number of bound variables does not match number of tokens”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18773467/