1

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);
$insertK->excecute();

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?

2
  • 1
    See stackoverflow.com/questions/6346674/… Commented Feb 5, 2013 at 5:35
  • 1
    As far as I know the first insert should block (the script should wait) until the query is finished. I could be wrong though. Commented Feb 5, 2013 at 5:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for telling Just run all your queries one by one usual way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.