这是该课程的代码:
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); }///从数据库中获取所有子代ID,并将它们存储在数组中
// Get all the Children IDs from the DB and store them in the array
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; }///将数组放大为id字符串,然后将其放入查询中
// Implode the array into an id string and throw it in the query
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{ } } }问题是,这总是以最后一个ID被删除而结束.该查询似乎正在工作,但是因为如果我将其回显并用$ id替换:id,它看起来会很好.
The thing is that this always ends up with only the last ID being deleted. The query seems to be working however because it looks totaly fine if i echo it and replace :id with $id.
//如果回显了SQL输出字符串:
// SQL output string if echoed:
DELETE FROM `shop_categories` WHERE id IN (11,6)//如果我手动将其添加到数据库中,则它将按预期工作,因此问题必须出在PDO语句中……有人可以帮助我吗?
// 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?
推荐答案您可以使用 FIND_IN_SET 为此:
You can use FIND_IN_SET for that:
DELETE FROM `shop_categories` WHERE FIND_IN_SET(id, :id)"更多推荐
带有in()的PHP PDO和DELETE无法正常工作
发布评论