如何使用PDO更新数据库? [英] How to update a database with PDO?
问题描述
我做一个print_r($params);
,每个值都通过.由于某种原因,它没有更新数据库.
I do a print_r($params);
and every value comes through. For some reason it isn't updating the database.
Select语句可以与其他PDO语句一起使用,并且可以使用mysql更新项目,但可以将其升级为PDO(例如此语句).
Select statements work with my other PDO statements and I can update items with mysql, but am upgrading those to PDO (such as this statement).
我的语法有问题吗?
$getData = new Data();
$gptest = $getData -> insert_group_id($item_id, $id, $gp, $groupid, $type);
Data()类包括以下内容:
Data() class includes these:
function insert_group_id($item_id, $id, $gp, $groupid, $type) {
$params = array(
'item_id' => $item_id,
'id' => $id,
'gp' => $gp,
'group_id' => $groupid,
'type' => $type
);
$qry = "UPDATE actions_item
SET gp = :gp ,
group_id = :group_id ,
type = :type
WHERE itemID = :item_id
AND itemVID = :id
";
return $this->update($qry, $params);
}
protected function update($sql, $params)
{
$stmt = $this->dbh->prepare($sql);
return $stmt->execute($params);
}
推荐答案
您的数组键必须具有与占位符相同的格式.
Your array keys need to have the same format as the placeholders.
$params = array(
':item_id' => $item_id,
':id' => $id,
':gp' => $gp,
':group_id' => $groupid,
':type' => $type
);
根据我在写完这篇文章后立即学到的知识,不需要 .我用所有相同的列名建立了一个表,没有它们,您的确切代码对我有用.因此,我真的不确定为什么这似乎可以解决问题.
Based on what I learned instantly after writing this, the colons shouldn't be required. I set up a table with all your same column names and your exact code worked for me without them. So, I'm really not sure why this seemed to solve the problem.
这篇关于如何使用PDO更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!