php - How can I add to row with highest primary key?

609

I have a database, with a column I would like to update where the row is the most recently generated one, after searching for a while I found this solution

UPDATE data SET Tricked = 'Affirmative' WHERE Identification = (SELECT MAX(Identification) FROM data);

However this didn't work, I received no errors but it did nothing. What about it isn't allowing it to work?

471

Answer

Solution:

MySQL is objecting to your use of theSELECT statement in the where clause on the table you're updating. You should see a response like this:

Error Code: 1093. You can't specify target table 'data' for update in FROM clause

PHP solutions involving thelast_insert_id only work for the lifetime of the session, and then only if no other user is inserting records, so won't be reliable.

Try this:

UPDATE data
  SET Tricked='Affirmative' 
  ORDER BY Identification DESC
  LIMIT 1

People are also looking for solutions to the problem: how to send mysql fetch records php page via email

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.