PDO prepared statement returns not correct lastInsertId

97 Views Asked by At

My head is close to explode.

I'm trying to insert a lot of rows to database using prepared statements (it's MODx cms).

Query looks like:

INSERT INTO {$table} (`fio`, `address`, `code`) 
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE 
   fio = VALUES(fio),
   address = VALUES(address),
   code = VALUES(code),
   `eid` = LAST_INSERT_ID(eid);

Next step is

foreach ( $data as $k=>$v){
   $q->execute(array($v['fio'], $v['address'], $v['code']));
   $eid = $this->modx->lastInsertId('eid');
}

And I have some rows in the table, that were filled before. When I'm trying to execute the script, only the first $eid is correct and points to an existing row. Every next value of $eid is more than even AUTO_INCREMENT value for the table.

Tried different variants, but $eid is correct only when I'm deleting prepared statement and using a usual query.

Help please...

0

There are 0 best solutions below