带有in()的PHP PDO和DELETE无法正常工作

编程入门 行业动态 更新时间:2024-10-25 20:26:06
本文介绍了带有in()的PHP PDO和DELETE无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是该课程的代码:

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无法正常工作

本文发布于:2023-10-12 16:35:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   PHP   PDO   DELETE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!