Here is what i'm trying to do:
Delete a project from the projects
table and all the images associated with that project in the images
table.
Lets say $del_id = 10
DELETE FROM projects, images WHERE projects.p_id = '$del_id' AND images.p_id = '$del_id'
What is wrong with this query?
The answer
The test
projects
images
the delete
the result
works a treat!
When being deleted, an item will never meet both of these requirements, therefore it must be
OR
notAND
(Wrong answer, MySQL allows this)
You can't delete from two tables in one query.
The closest you can get is wrap the two deletes in a transaction:
As Chacha102 noted, the problem of your query was the
AND
in theWHERE
clause.However, you may want to use the
JOIN
syntax for multi-tableDELETE
s, which I find easier to read:Change the AND into an OR.
You might want to use a foreign key constraint with a cascading delete, much easier, but you have to use innoDB and create this FK-constraint. Delete the project and all related images will be deleted as well.