Take the 2-minute tour ×

Im trying to build a form to allow binding of keywords to articles. This SQL statement works directly as a query but I dont know how to package it as a pdo statement. It adds the keyword to a Keyword Table and Keyword ID + Article ID to a many to many mapping table.

$insertK = $dbh->prepare("INSERT IGNORE INTO Keywords (Keyword)
VALUES (:KeywordID1);
INSERT INTO Keyword_Article (KeywordID, ArticleID)
VALUES ((SELECT KeywordID FROM Keywords WHERE Keyword = :KeywordID2), :ArticleID)");

$insertK->bindParam(':KeywordID1',  $keywordID);
$insertK->bindParam(':KeywordID2',  $keywordID);
$insertK->bindParam(':ArticleID',  $articleID);

Ive seen PDO inserts a few different ways but none that do two statements into two different tables.

EDIT* If its not possible then how could I make sure that the first insert is finished before running the second query?

share|improve this question
As far as I know the first insert should block (the script should wait) until the query is finished. I could be wrong though. –  John V. Feb 5 '13 at 5:40
add comment

1 Answer

up vote 3 down vote accepted

This is quite common misconception.
For some reason people constantly trying to stuff as many queries in one call as possible.
While there is actually no reason at all.

Just run all your queries one by one usual way.
There is absolutely nothing wrong with it.

share|improve this answer
+1 for telling Just run all your queries one by one usual way. –  NullPoiиteя Feb 5 '13 at 5:43
add comment

Your Answer


