带有in()的PHP PDO和DELETE无法正常工作 [英] PHP PDO and DELETE with in() not working

查看:157
本文介绍了带有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?

推荐答案

您可以使用

You can use FIND_IN_SET for that:

DELETE FROM `shop_categories` WHERE FIND_IN_SET(id, :id)"

这篇关于带有in()的PHP PDO和DELETE无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆