Jump to content

get id after update


Destramic

Recommended Posts

is it possible to get the value of user_id when updating a row?..this query works but im struggling to get the user_id (doesnt return a result)

UPDATE users 
SET status = 'Active',
             token = NULL 
WHERE token = 'test' 
AND status = 'Blocked' 
AND (SELECT user_id as `user_id`)

if i'm not able to do it that way then i could use the following, but would be great if i was able to do this all in one query.

SELECT LAST_INSERT_ID();

thank you

Link to comment
https://forums.phpfreaks.com/topic/296803-get-id-after-update/
Share on other sites

If there is a way I am not aware of it. LAST_INSERT_ID (as it says in the tin) only works for insert statement, returning the value of the auto_incremented id.

 

You may have to do this first to see which ones will be updated

SELECT user_Id 
FROM users
WHERE token = 'test'
AND status = 'Blocked'

but it depends on what you want to do with the user_id once you know it. It may be that a trigger function is required if, say, you want to log the id.

Link to comment
https://forums.phpfreaks.com/topic/296803-get-id-after-update/#findComment-1513837
Share on other sites

well the user will go to the url which will have a token in it...if token exists then it will unblock account.

 

now i want the user_id so i can send the user to the change password action...I'm passing the user_id as a parameter so if the  $user_id parameter is not null in the change password action he/she can change password without logging in.

 

else if null and not authenticated then redirected to log in...if that sounds right

 

i couldnt think of another way of letting the user change password without being authenticated.

Link to comment
https://forums.phpfreaks.com/topic/296803-get-id-after-update/#findComment-1513842
Share on other sites

actually, if your UPDATE query contained another SET term of - user_id = LAST_INSERT_ID(user_id), your second query of SELECT LAST_INSERT_ID() would return the user_id value.

 

edit: and you don't actually need to explicitly run the second query, you can just call the lastInsertId.. function/method of whatever php database library you are using.

Link to comment
https://forums.phpfreaks.com/topic/296803-get-id-after-update/#findComment-1513845
Share on other sites

actually, if your UPDATE query contained another SET term of - user_id = LAST_INSERT_ID(user_id), your second query of SELECT LAST_INSERT_ID() would return the user_id value.

 

edit: and you don't actually need to explicitly run the second query, you can just call the lastInsertId.. function/method of whatever php database library you are using.

 

worked like a dream thank you...its always fun to find easier ways of doing things :)

Link to comment
https://forums.phpfreaks.com/topic/296803-get-id-after-update/#findComment-1513987
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.