StudioTime
StudioTime

Reputation: 23979

MySQL - ...on duplicate key not updating

I have a table user_passwords:

user_password_id AI PK
hashPass varchacr(255)

When a user resets their password I want to update the row, so this should, and does fail:

insert into user_passwords (user_password_id,hashedPassword)
VALUES('2','$2y$11$pVYR/0hcgOewMn2jgrGx.uGcky5TXxYOPvsbWGyH3VQxZlj3c1QD.')

Error Code: 1062. Duplicate entry '2' for key 'PRIMARY'

I know that, so I'm trying the following but no rows get updated:

insert into user_passwords (user_password_id,hashedPassword)
VALUES('2','$2y$11$pVYR/0hcgOewMn2jgrGx.uGcky5TXxYOPvsbWGyH3VQxZlj3c1QD.')
on duplicate key update hashedPassword=hashedPassword

Why is that?

Upvotes: 1

Views: 47

Answers (1)

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

You are looking for this:

on duplicate key update hashedPassword = VALUES(hashedPassword)

see also the docs here: http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

Upvotes: 3

Related Questions