Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php mysql中删除带有子类别的类别时,如何更新帖子类别关系?_Php_Mysql - Fatal编程技术网

在php mysql中删除带有子类别的类别时,如何更新帖子类别关系?

在php mysql中删除带有子类别的类别时,如何更新帖子类别关系?,php,mysql,Php,Mysql,在我的博客系统中,我有多对多的关系。我有三张桌子。tb_类别、tb_帖子和tb_帖子与cat_关系。下文给出了这些建议- 结核病类别- // update post category relationship public function update_post_cat_relationship($where) { if(is_array($where) && !empty($where)) { $cat_id = implode(", ", $

在我的博客系统中,我有多对多的关系。我有三张桌子。tb_类别、tb_帖子和tb_帖子与cat_关系。下文给出了这些建议-

结核病类别-

// update post category relationship
public function update_post_cat_relationship($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` = " . $where;
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
}

// delete category and subcategories    
public function delete_category($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` IN (" . $cat_id . ")";
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` = " . $where;
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
结核分枝杆菌员额-

tb_后_猫_关系-

您可以理解这三个表之间的关系。现在,当一个类别被删除时,属于该类别的帖子将被分配给未分类的帖子,其id为-1,并且它不在tb_categories表中。如果父类别被删除,则其所有子类别也将被删除,但属于父类别和子类别的所有帖子都将与未分类(即-1 id)关联

我的问题开始了。如果不同的帖子与这些父类别和子类别相关联,那么其ok-i将所有帖子更新为与这些类别ID对应的-1。但是,如果一篇文章与多个类别及其子类别关联,则会创建大量重复行,因为id为1的一篇普通文章可能与类别1及其子类别2、3、4等关联。因此,将创建4行重复数据,如

因为我删除了父类别Web Development,所以它的子类别PHP也被删除,因此属于这两个类别的帖子现在被分配到-1未分类类别

这就是问题所在。我只想要一个帖子的唯一行。因为现在分享多个类别并分配给Uncategorized-1,所以应该只有一行像这样-

我怎样才能解决这个问题?下面给出了我的PHP代码-

当我删除类别时,此代码被调用-

    $where = $_GET['cat_id'];
    $status = $db_obj->update_post_cat_relationship($where)->delete_category($where);
这是更新表格和删除类别的代码-

// update post category relationship
public function update_post_cat_relationship($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` = " . $where;
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
}

// delete category and subcategories    
public function delete_category($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` IN (" . $cat_id . ")";
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` = " . $where;
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
可以与级联删除一起使用。删除时,将删除关系或设置空值