christian
christian

Reputation: 2489

Updating Timestamp Fields with Current Time, but not on every row update

I have two TIMESTAMP fields in my user table that I want to update with the current timestamp, one when the row/account is created and the other every time a user logs in.

I am having trouble updating using:

UPDATE user SET last_login = NOW() WHERE id = 1

and

UPDATE user SET created = NOW() WHERE id = 1

The queries are not having any affect. I'm thinking this is an issue with the table setup and have tried many different options.

1 is the id of the user and is hardcoded for this example. I have also tried hardcoding the value in my code and checking to make sure my code reaches the query. Still no dice.

Any ideas what I should set my table fields to?

Upvotes: 0

Views: 756

Answers (2)

Álvaro González
Álvaro González

Reputation: 146460

Be aware that TIMESTAMP columns can be set to update automatically, thus overriding your manual changes:

The TIMESTAMP data type offers automatic initialization and updating. You can choose whether to use these properties and which column should have them:

http://dev.mysql.com/doc/refman/5.5/en/timestamp.html

Make sure you haven't enabled any of these properties (or switch to another data type that does not have such features).

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65274

Your fields shouldn't be of type TIMESTAMP, but of type DATETIME

Upvotes: 1

Related Questions