Roger
Roger

Reputation: 1459

Add CURRENT_TIMESTAMP to MySQL record on update

I'm updating MySQL records:

mysql_query("UPDATE nodes SET text='". $text . ..... "', datealtered='CURRENT_TIMESTAMP', ..... '") or die(mysql_error());    

I set the type for datealtered in PHPMyAdmin to CURRENT_TIMESTAMP. All of the other fields get updated, but the Date never gets updated. What am I doing wrong?

Upvotes: 1

Views: 4042

Answers (1)

mu is too short
mu is too short

Reputation: 434845

This:

datealtered='CURRENT_TIMESTAMP'

Is trying to set datealtered to the literal string 'CURRENT_TIMESTAMP' not the value of the CURRENT_TIMESTAMP function (AKA now()). Try dropping the single quotes to get at the current timestamp (rather than a string):

datealtered=CURRENT_TIMESTAMP

MySQL tends to silently ignore errors so it is probably trying to interpret 'CURRENT_TIMESTAMP' as a date and silently failing.

And I hope you're properly escaping $text and friends to avoid SQL injection attacks and similar unpleasantness.

Upvotes: 6

Related Questions