php - Delete a database record and associated image

234

I have a table of photos like this:

photo_id, photo_url, user_id

When I want to delete a photo, I need to delete the row in the table and the photo on the server. So to do this, I need to do 2 queries:

  1. ASELECT query to return the photo's url and delete it withunlink()
  2. aDELETE query to delete the row in the table

Is a simpler way to do this, perhaps using only one query?

755

Answer

Solution:

Your way the best way to do this....How can it be simpler....

Also, if your photos use ids then you may just delete the row instead of selecting the query to return to the photos URL. then if this is successful proceed with deleting the thumbnail

People are also looking for solutions to the problem: php - Singleton session problem

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.