Home  >  Article  >  Backend Development  >  Deletion problem - How to delete product categories in php?

Deletion problem - How to delete product categories in php?

WBOY
WBOYOriginal
2016-09-01 00:20:161075browse

<code>您的位置:商品分类栏目
分类ID    分类树    分类名称    父类ID    父类路径    商品分类管理

6  家电    0    0,    修改 | 复制 | 删除

15 黑白电视机    6    0,6,    修改 | 复制 | 删除

27 洗衣机    6    0,6,    修改 | 复制 | 删除

28 液晶电视机    6    0,6,    修改 | 复制 | 删除

3  女装    0    0,    修改 | 复制 | 删除

12 休闲裤    3    0,3,    修改 | 复制 | 删除

7 手机    0    0,    修改 | 复制 | 删除

8 iphone    7    0,7,    修改 | 复制 | 删除

10 iphone6s    8    0,7,8,    修改 | 复制 | 删除

9 华为    7    0,7,    修改 | 复制 | 删除

11 华为mate9    9    0,7,9,    修改 | 复制 | 删除

19 vivo    7    0,7,    修改 | 复制 | 删除

26 vivoX7    19    0,7,19,    修改 | 复制 | 删除


//上表怎么做商品分类删除?删除父级分类=父级+子级
$id=$_GET['id'];
        $sql="select path from type WHERE path like '%$id%'";
        $rs=mysql_query($sql);
        $arr=mysql_fetch_assoc($rs);
       if($arr){
          $path=" and path like '%$id%'";
        }else{
            $path='';
        }
        $where="where id={$id}{$path}";
        $sql2="delete from type $where";
        //echo $sql2;die();
        $rs=mysql_query($sql2);
        if(mysql_affected_rows()>0){
            echo '删除成功';
        }else{
            echo '删除失败';
        }
        break;
//我写的代码为什么不能删除有子级分类的商品呢?只能把子分类删完才能执行父类
//是where and的前后顺序问题吗?

比如删除女装是,需要先把休闲裤删了才行,但我想直接删完,应该怎么做呢??</code>

Reply content:

<code>您的位置:商品分类栏目
分类ID    分类树    分类名称    父类ID    父类路径    商品分类管理

6  家电    0    0,    修改 | 复制 | 删除

15 黑白电视机    6    0,6,    修改 | 复制 | 删除

27 洗衣机    6    0,6,    修改 | 复制 | 删除

28 液晶电视机    6    0,6,    修改 | 复制 | 删除

3  女装    0    0,    修改 | 复制 | 删除

12 休闲裤    3    0,3,    修改 | 复制 | 删除

7 手机    0    0,    修改 | 复制 | 删除

8 iphone    7    0,7,    修改 | 复制 | 删除

10 iphone6s    8    0,7,8,    修改 | 复制 | 删除

9 华为    7    0,7,    修改 | 复制 | 删除

11 华为mate9    9    0,7,9,    修改 | 复制 | 删除

19 vivo    7    0,7,    修改 | 复制 | 删除

26 vivoX7    19    0,7,19,    修改 | 复制 | 删除


//上表怎么做商品分类删除?删除父级分类=父级+子级
$id=$_GET['id'];
        $sql="select path from type WHERE path like '%$id%'";
        $rs=mysql_query($sql);
        $arr=mysql_fetch_assoc($rs);
       if($arr){
          $path=" and path like '%$id%'";
        }else{
            $path='';
        }
        $where="where id={$id}{$path}";
        $sql2="delete from type $where";
        //echo $sql2;die();
        $rs=mysql_query($sql2);
        if(mysql_affected_rows()>0){
            echo '删除成功';
        }else{
            echo '删除失败';
        }
        break;
//我写的代码为什么不能删除有子级分类的商品呢?只能把子分类删完才能执行父类
//是where and的前后顺序问题吗?

比如删除女装是,需要先把休闲裤删了才行,但我想直接删完,应该怎么做呢??</code>

delete from table where id=7 or path like '%,7,%'

Based on my experience, I have the following suggestions:
1. When deleting any category of product categories, you must check the database to verify whether there are subcategories under the category. If so, the category cannot be deleted.
2. Before deleting this category, you must delete all subcategories under this category, and search the database according to the category parent ID.
3. If you do not follow the above aspects, junk data will definitely be generated.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn