php - lastInsertId() returns 0 -
php - lastInsertId() returns 0 -
i have looked through similar questions here have not been able solve problem…all help appreciated!
basically, have next sql code in php:
$query = 'create temporary table tmp select * orders id='.$old_order_id.'; alter table tmp drop id; # drop autoincrement field update tmp set groupid=null, timeordered=current_timestamp, totalorderstatus=0; insert orders select 0,tmp.* tmp; drop table tmp;'; $db->exec($query); $new_order_id = $db->lastinsertid(); now, have thought lastinsertid() homecoming id of row lastly inserted 'orders'. instead, zero--but don't know whether because taking lastly inserted id of table 'tmp', or if it's because of other error i'm making. (i don't see why take id tmp, since that's not lastly inserted id really...)
it looks trying re-create existing row table info modified. why don't simplify , like:
insert orders ( /* fields changing */ groupid, timeordered, totalorderstatus, /* other fields select below not changing */ field1, field2, ... ) select /* values fields changing */ null, current_timestamp, 0, /* other fields order table remain unchanged */ field1, field2, ... orders id = ? just don't insert value whatever primary key field (assuming autoincrement), insert id work fine.
php mysql lastinsertid
Comments
Post a Comment