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