[英]PHP PDO and DELETE with in() not working
Here is the code for the class: 这是该类的代码:
class Delete_Category extends Category {
private $idchain = array();
public function __construct($id) {
parent::__construct();
$this->check_id($id);
$this->get_delete_ids($this->id);
$this->delete_category($this->idchain);
}
// Get all the Children IDs from the DB and store them in the array //从数据库中获取所有子代ID并将其存储在数组中
private function get_delete_ids($id) {
$this->query = $this->db->prepare("SELECT id FROM `shop_categories` WHERE parent_id = :id");
$this->query->execute(array("id" => $id));
while($result = $this->query->fetch(PDO::FETCH_ASSOC)) {
$this->get_delete_ids($result['id']);
}
$this->idchain[]= $id;
}
// Implode the array into an id string and throw it in the query //将数组放大为id字符串,然后将其放入查询中
private function delete_category($id_array) {
$id = implode(",",$id_array);
try {
$this->query = $this->db->prepare("DELETE FROM `shop_categories` WHERE id IN (:id)");
$this->query->execute(array(':id' => $id));
}
catch(PDOException $e) {
// log it{
}
}
}
The thing is that this always ends up with only the last ID being deleted. 事实是,这总是最后只会删除最后一个ID。 The query seems to be working however because it looks totaly fine if i echo it and replace :id with $id.
该查询似乎正在工作,但是因为如果我将其回显并将$:id替换为:id,它看起来会很好。
// SQL output string if echoed: // SQL输出字符串(如果回显):
DELETE FROM `shop_categories` WHERE id IN (11,6)
// If i manually add this to the Database it works as intended so the problem has to be somewhere at the PDO statement... Can anyone help me? //如果我手动将此添加到数据库中,它将按预期工作,因此问题必须出在PDO语句中某处...有人可以帮助我吗?
您可以FIND_IN_SET
使用FIND_IN_SET
:
DELETE FROM `shop_categories` WHERE FIND_IN_SET(id, :id)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.