Skip to content
Advertisement

LAST_INSERT_ID not working on UPDATE

if I use this SQL:

UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID();

I will get 0 as last insert id. I’m using php mysqli_insert_id and here is said that:

The mysqli_insert_id() function returns the ID generated by a query 
on a table with a column having the AUTO_INCREMENT attribute. 
If the last query wasn't an INSERT or UPDATE statement 
or if the modified table does not have a column with the AUTO_INCREMENT attribute, 
this function will return zero. 

my table formualre has auto increment column, so I don’t know wher the problem is

Advertisement

Answer

LAST_INSERT_ID() won’t work if no new auto increment value was created.

The solution is something like this:

UPDATE formulare
  SET EV_id=LAST_INSERT_ID(59),
    EV_status=5
  WHERE EV_id=57
    AND ID_uziv=12;
SELECT LAST_INSERT_ID();

Note: I guess, that EV_id is the auto_increment primary key.

Otherwise you should do a query like:

UPDATE formulare
  SET key_col = LAST_INSERT_ID(key_col),
    EV_id=59,
    EV_status=5
  WHERE EV_id=57
    AND ID_uziv=12;
SELECT LAST_INSERT_ID();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement