php - Last insert id issue--How to fetch it -


I enter $ id = mysqli_insert_id ($ connection) to enter the previous code; I'm using , but if it updates any record in the table, it returns 0 as the last inserted id.

Is there any way to handle this?

I have to get the ID every time to insert or update the weather.

Thanks

Edit

I need this ID to use in the data table2

insert data from tab2 to ID to enter data from ID1 tab 1 < / Code> is FK

And most importantly, I am not using with update , where section

This is my code that I am using

  $ val = utf8_encode ($ val); Mysqli_set_charset ($ connection, 'utf8'); MySqli_query ($ connection, "SET NAMES 'utf8'); Mysqli_query ($ connection," SET FOREIGN_KEY_CHECKS = 0; "); $ Sql ​​=" Enter in the leak ($ ​​inserted) value ($ val) "; $ Sql = "Key Update of Duplicate 'URL` ='" .mysqli_real_escape_string ($ connection, $ data ['url']). ''; '; Mysqli_query ($ connection, ($ sql)) or die (mysqli_error ($ connection). "& Lt; br />".print($sql)); $ Id = mysqli_insert_id ($ connection); $ Proof ['leaks_id'] = $ id; Mysqli_query ($ connection, "SET FOREIGN_KEY_CHECKS = 0;"); Print_r ($ id); $ This- & gt; InsertProofs ($ connection, $ proof); Connection :: close_connection ($ connection);  

Please note that $ this-> InsertProofs ($ connection, $ proof); insert data near table2 near key

on INSERT

> UPDATE

on

depending on your updates;

  • id will know if you are updating
  • Find out the criteria for the ID to search for updates .

For example, if your UPDATE was something like this;

UPDATE `foo` SET`x` = 'y' where 'a` =' b '

You can then run

To enter an update ID, select `ida``a '=' b '

.

Edit

I see that you are using a duplicate key update on .

You can modify your query (assuming that the id is the primary auto_interpret key)

  on the duplicate key update `url `= '" .mysqli_real_escape_string ($ connection, $ data [' url ']).' For example, if you have  UPDATE  or  INSERT  

for example, id = LAST_INSERT_ID (id)

If I run (if we exist with the record of id = 2 ; so we have update );

  FuBar (id, foo) Update the duplicate key in VALUES (2, 'bar'), INSERT at foo = 'baz'. Id = LAST_INSERT_ID (id); SELECT LAST_INSERT_ID ();  

Output 2, because it was the last insert id.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -