如何使用pdo删除类别及其数据?

时间:2018-05-15 16:58:48

标签: php mysqli pdo

我正在尝试使用INNER JOIN从数据库中删除类别及其数据。 但有错误的参数错误。 这是错误:

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in D:\wamp\www\p\admin\DelCat.php:25 Stack trace: #0 D:\wamp\www\p\admin\DelCat.php(25): PDOStatement->execute(Array) #1 {main} thrown in D:\wamp\www\p\admin\DelCat.php on line 25

我使用了mysqli oop中使用的参数,不知道如何在pdo中提供参数。

这是删除页面中的代码

if (isset($_GET['cat_id']) && is_numeric($_GET['cat_id'])){
    $catid = filterString($_GET['cat_id']);
    $stmt = $pdo->prepare('DELETE FROM categories AS c 
                            INNER JOIN products AS p ON p.cat_id = :c.catid
                            INNER JOIN orders AS o ON o.cat_id = :c.cat_id WHERE c.cat_id = :p.catid AND c.cat_id = :o.cat_id ');
    $del = $stmt ->execute(array('cat_id' =>$catid, 'catid' =>$catid, 'catid' =>$catid));
    if($del){
        $_SESSION['message'] = "Category Deleted!";       
        header("location: succes.php");
        exit(); 
    }else{
        $_SESSION['message'] = "Something went wrong!";       
        header("location: error.php");
        exit(); 
    }
}

第二次尝试: 我试过这样很好,它将我重定向到error.php而没有给出任何错误。

if (isset($_GET['cat_id']) && is_numeric($_GET['cat_id'])){
    $catid = filterString($_GET['cat_id']);
    $sql = "DELETE FROM categories 
                        INNER JOIN products ON products.cat_it = categories.cat_id
                        INNER JOIN orders ON orders.catid = categories.cat_id 
                        WHERE categories.cat_id = ?  AND products.catid = ? AND  orders.cat_id = ?";
        $stmt = $pdo->prepare($sql);
        $del = $stmt ->execute($catid);
    if($del){
        $_SESSION['message'] = "Category Deleted!";       
        header("location: succes.php");
        exit(); 
    }else{
        $_SESSION['message'] = "Something went wrong!";       
        header("location: error.php");
        exit(); 
    }
}

0 个答案:

没有答案