xRobot
xRobot

Reputation: 26567

How to do these 2 queries at once?

In my database there are 3 tables:

Posts:
-id
-title
-body
-date

Tags:
-id
-title

Posts_Tags:
-id
-post_id
-tag_id

When an user insert a post, my php code does 2 queries:

  1. Insert the post in the Posts table
  2. Insert a row for every tags associated with that post in the Posts_Tags table.

Is this the correct approach ?

Is there a way to simplify or to do 1 single query ?

Upvotes: 0

Views: 85

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

Is correct approach provided you do it into a transaction (atomic operation); that is, if you insert a post and there's an error into tags insertion (or in tag association insertion), also post will not be inserted

Update

After xRobot comment, I'd updated my answer with this

Upvotes: 2

Related Questions