1

I'm using PDO::prepare() to prepare an INSERT statement. PDO::execute() returns true, and $dbh->lastInsertId() always has the next id, but no row is actually inserted!

What could be causing this?

2 Answers 2

1

if $dbh->lastInsertId() returns id from this very insert (i.e. no other inserts were fired), the row was apparently inserted. Just double-check your table. I mean it.

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

Comments

0

I'm going to stab in the dark here and wager that you didn't run $dbh->commit() afterwards. Without the commit it will roll back the changes once your script ends or the connection is closed.

See also: Transactions and auto-commit

2 Comments

I thought perhaps this was the case, since the second insert has a foreign-key constraint that depends on the success of the first insert. I've added a $dbh->commit() after the first insert, and it does show up correctly, but the second insert still throws a foreign-key constraint error. Any ideas why?
@AlienBishop In that case you would need to give more details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.