Wessel Maan
Wessel Maan

Reputation: 1

Is it possible to put two INSERT statements in one SQL query

I'm trying to get two INSERT statement into one SQL query. I tried several things but nothing seems to work. If I look on the internet it seems it has to work. But it doesn't work for me and I get an error. I'm using this code with PDO.

I tried multiple web browsers, multiple types of code and I tried the "BEGIN" and "COMMIT" statement.

$sql = "INSERT INTO gebruiker (email, wachtwoord, rol_id) VALUES (?, ?, 2); ".
"INSERT INTO huisarts (voornaam, achternaam, email, straatnaam, huisnummer, postcode, plaats, telefoonnummer, uid, rol_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, LAST_INSERT_ID(), 2);";
$pdostatement = $pdo->prepare($sql);

Upvotes: 0

Views: 3032

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157876

That silly concatenation mistake aside, it is technically possible to run two INSERT queries in one call, but there is not a single reason to do so.

Besides, you will need to configure PDO specifically to allow multiple queries which is not advised.

Therefore, just run these two queries as two separate prepared statements.

$sql = "INSERT INTO gebruiker (email, wachtwoord, rol_id) VALUES (?, ?, 2)";
$stmt = $pdo->prepare($sql);
$stmt->execute($first_query_data);
$sql = "INSERT INTO huisarts (voornaam, achternaam, email, straatnaam, huisnummer, postcode, plaats, telefoonnummer, uid, rol_id) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, LAST_INSERT_ID(), 2);";
$stmt = $pdo->prepare($sql);
$stmt->execute($second_query_data);

Upvotes: 3

Related Questions