仅当类别没有子类别时才尝试删除类别

时间:2014-04-01 23:57:35

标签: php mysql pdo

我有类别和子类别,我试图建立一个系统,如果我想要删除一个类别,我只能在第一次删除子类别时才能这样做。

我的子类别具有类别的id_father。

喜欢这样:

(警告和技术是类别)

enter image description here

我的问题:

当我删除带有子类别的类别时,我的错误消息显示在下面的代码中(此类别具有子类别),并且该类别实际上已从我的数据库中删除。

我已多次查看代码,但我无法理解错误的位置。

你能帮我一点吗?

我的PHP代码:

if(!empty($_GET['deleteCategory']))
    {   
     $idDel = $_GET['deleteCategory'];
     $readDeleteCategory = $pdo->prepare("SELECT * FROM categories where id_father = ?");  
     $readDeleteCategory->bindValue(1, $idDel);
     $num_rows_ReadDeleteCategory = $readDeleteCategory->rowCount();
     //if dont return results we have the delete and show the message
    if(! $num_rows_ReadDeleteCategory >=1)
        {
        $deleteCategory = $pdo->prepare("DELETE FROM categories WHERE id =?");
        $deleteCategory->bindValue(1,$idDel);
        $deleteCategory->execute();
        echo 'category removed with sucess.';   
            }
    else
        {
        echo 'This category has subcategories. Remove subcategories first.';
        }
     }


if(!empty($_GET['deleteSubcategory']))
    {
    $idDel = $_GET['deleteSubcategory'];
    $deleteCategory = $pdo->prepare("DELETE FROM categories WHERE id =?");
    $deleteCategory->bindValue(1,$idDel);
    $deleteCategory->execute();
    echo 'Category removed with sucess.';
    }

1 个答案:

答案 0 :(得分:1)

我认为你只是错过了一个简单的界限。

 $readDeleteCategory->bindValue(1, $idDel);
 $readDeleteCategory->execute();
 $num_rows_ReadDeleteCategory = $readDeleteCategory->rowCount();