Reputation: 574
I have two tables, news and news_links. The news table looks like this (id, title, body, date_published) and the news_links look like this (id, news_id, url).
I'm trying to build a query where I can insert a news array:
array(
array(
"id" => "123456",
"title" => "First Title",
"body" => "First body text",
"date_published" => "01-01-16"
)
array(
"id" => "22222",
"title" => "Second Title",
"body" => "Second body text",
"date_published" => "01-01-16"
)
)
This array will contain multiple array, this is just an example.
Links related to these news will also be inserted.
array(
array(
"0" => "1",
"1" => "123456",
"2" => "img_url"
)
array(
"0" => "2",
"1" => "123456",
"2" => "img_url"
)
array(
"0" => "2",
"1" => "22222",
"2" => "img_url"
)
)
Will I have to build a foreach loop and run multiple queries or is there a way to run a single query and insert everything into both news and news_links?
Upvotes: 0
Views: 591
Reputation: 7504
No, you can't use one query to insert data in two different tables. However, you can combine several inserts for one table in one query. It will look like
INSERT INTO news_links VALUES(1,123456,'url'),(2,123456,'url'),(2,22222,'url');
Upvotes: 1