1
我正在创建一个使用PHP PDO将数据插入数据库的动态函数。为什么我得到的绑定变量数量与令牌数量错误消息不匹配?
以下是Database
类中的函数。
public function insert ($table, $feilds = array()) {
$key = array_keys($feilds);
$column = implode(',', $key);
$ques = array();
foreach ($feilds as $value) {
$ques[] = " ?";
}
$ques = implode(', ', $ques);
$stmt = $this->_pdo->prepare("INSERT INTO $table($column) VALUES ($ques)");
$x=1;
foreach ($feilds as $key => $value) {
$stmt->bindParam($x, $var);
$var = $value;
$stmt->execute();
$x++;
}
}
我插入数据是这样的:
$data = array(
'b_title' => 'My blog title',
'b_des' => 'My blog description',
);
$database->insert('blog', $data);
但是当我运行此查询它的说法
非法参数编号:绑定变量的数量不符 数令牌
我不明白在哪里绑定变量和标记不匹配!我该如何解决它?
谢谢。
OH之外,我瞎:( –
它发生在每个人:P – thelmuxkriovar