How to delete rows in multiple tables using pdo

ismail tunahan

I am trying to delete category and products belong to it in products_TABLE using pdo INNER JOIN. it works if I delete only category without deleting products. Here is my code :

$catid = filterString($_GET['cat_id']);
$stmt = $pdo->prepare('DELETE FROM categories AS c
                        INNER JOIN products AS p ON c.cat_id = p.catid
                        WHERE cat_id = :cat_id
                        ');
$delete = $stmt->execute(array('cat_id' =>$catid));

This is the error I am having :

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in D:\wamp\www\p\employees\DelStore.php:25 Stack trace: #0 D:\wamp\www\p\employees\DelStore.php(25): PDOStatement->execute(Array)

1 {main} thrown in D:\wamp\www\p\employees\DelStore.php on line 2

I understand it says given parameters are invalid, But couldnt solve how to give parameters in:

$delete = $stmt->execute(array('cat_id' =>$catid));

Thanks for any advice

Dimitris Filippou

The error you are getting is because you have :products.catid Change that to products.catid

Also if you want to delete the entries from both tables you shoul use aliases.

DELETE c,p FROM categories c
INNER JOIN products p ON c.cat_id = p.catid
WHERE cat_id = :cat_id

Also you need to change

$delete = $stmt->execute(array('cat_id' =>$catid));

to

$delete = $stmt->execute(array(':cat_id' =>$catid));

The above example doesn't work if you are using a SQL Server. In that case you should be using 2 seperate delete queries.

Also when you bind parameters in the execute function as an array thay are binded as stings. This might also cause some problems depending on your database structure. Using $stmt->bindParam() is usually a better option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Updating multiple columns and rows using PDO

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Join two tables using multiple rows in the join

From Dev

Selecting multiple rows in a Select statement using PDO

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Delete multiple rows using IDs?

From Dev

Delete rows from multiple tables as one query

From Dev

How to delete multiple rows from multiple tables using Where clause?

From Dev

Delete Using Multiple Tables and repeat table in the subquery

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

Delete a row in multiple access tables using vb

From Dev

How to properly delete a row using PHP & PDO

From Dev

How to delete multiple rows with JdbcTemplate

From Dev

How can I delete rows from tables using EF when inside an Asp.Net method?

From Dev

MySQL delete multiple rows in multiple tables

From Dev

Delete rows from multiple tables in a database

From Dev

SQL, How to delete rows from related tables using a query?

From Dev

How can i delete from the database multiple rows using a checkbox?

From Dev

Delete multiple rows using IDs?

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

Insert multiple rows using PHP PDO

From Dev

Delete multiple rows using Jquery

From Dev

How to delete rows from two tables using INNER JOIN in mysql?

From Dev

How to delete multiple tables in SQLAlchemy

From Dev

How to get rows of Data from Multiple Tables using LinQ To Entities

From Dev

How to delete multiple rows using checkbox in Angular 2?

From Dev

How to delete multiple rows in diferent tables?

From Dev

How to remove null rows in when using union all in multiple tables

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

Related Related

  1. 1

    Updating multiple columns and rows using PDO

  2. 2

    MySQL - Delete from multiple tables using a UNION?

  3. 3

    Join two tables using multiple rows in the join

  4. 4

    Selecting multiple rows in a Select statement using PDO

  5. 5

    Delete multiple rows from multiple tables in mysql using single value

  6. 6

    Delete multiple rows using IDs?

  7. 7

    Delete rows from multiple tables as one query

  8. 8

    How to delete multiple rows from multiple tables using Where clause?

  9. 9

    Delete Using Multiple Tables and repeat table in the subquery

  10. 10

    Postgresql delete multiple rows from multiple tables

  11. 11

    Delete a row in multiple access tables using vb

  12. 12

    How to properly delete a row using PHP & PDO

  13. 13

    How to delete multiple rows with JdbcTemplate

  14. 14

    How can I delete rows from tables using EF when inside an Asp.Net method?

  15. 15

    MySQL delete multiple rows in multiple tables

  16. 16

    Delete rows from multiple tables in a database

  17. 17

    SQL, How to delete rows from related tables using a query?

  18. 18

    How can i delete from the database multiple rows using a checkbox?

  19. 19

    Delete multiple rows using IDs?

  20. 20

    DELETE rows from multiple tables with JOIN in Mysql

  21. 21

    Insert multiple rows using PHP PDO

  22. 22

    Delete multiple rows using Jquery

  23. 23

    How to delete rows from two tables using INNER JOIN in mysql?

  24. 24

    How to delete multiple tables in SQLAlchemy

  25. 25

    How to get rows of Data from Multiple Tables using LinQ To Entities

  26. 26

    How to delete multiple rows using checkbox in Angular 2?

  27. 27

    How to delete multiple rows in diferent tables?

  28. 28

    How to remove null rows in when using union all in multiple tables

  29. 29

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

HotTag

Archive