Reputation: 20872
I have the following code (removed the actual data) and it successfully writes a record into my table.
$db = new database('db_name');
$sql='INSERT INTO table (fieldshere);';
$pds=$db->pdo->prepare($sql);
$pds->execute(array(datahere));
$_SESSION['userid'] = $db->pdo->lastInsertId();
The problem I'm having is its returning the previous record number - eg: if it has just written record number 100 it returns number 99.
I can see auto increment on the table row number and this appears normal. eg: if table row is currently 100 then auto increment says 101 (for the next row).
The table started at number 0 but still not sure if this would be any issue.
Any thing is the table has a userid column which has auto increment on it and this is the number I need.
Is there any reason why I would be getting the previous row number?
thx
Upvotes: 2
Views: 402
Reputation: 8189
If you are using postgreSQL, you might have the same probleme posted here : Get last insert id after a prepared insert with PDO
you probably should specify the "name" parameter of the lastInsertId() method : "Name of the sequence object from which the ID should be returned."
Upvotes: 2