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
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
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
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.

Edited by mac_gyver
  • Like 2
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.