duanfaxin7014 2018-05-15 16:58
浏览 20

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

I am trying to delete a category and its data from database with INNER JOIN. But having wrong parameters error. Here is Error:

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

I used parameters like using in mysqli oop, dont know how to give parameters in pdo.

Here is my code in delete page

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(); 
    }
}

SECOND TRY : I tried like this is well, its redirecting me to error.php without giving any error.

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ogg dd trandata 报错
    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错