8

Basically I have a simple login form. In the database I have a 'last_logged' column and would like to update it with the current date and time every time someone logs in.

I currently have this query:

    UPDATE users SET last_logged = "NOW()" WHERE id = 1

But it doesn't update the column to the current date. Any ideas why?

Leigh
  • 28,765
  • 10
  • 55
  • 103
CarlTaylor1989
  • 209
  • 1
  • 4
  • 12

2 Answers2

21

Remove the quotes from NOW(). As a function call, it should be unquoted.

UPDATE users SET last_logged = NOW() WHERE id = 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
3

MS SQL uses GETDATE() rather than NOW()

(Just an FYI)
In SQL-Server I now use SYSDATETIME():

DECLARE @now DATETIME = DATEADD(dd,DATEDIFF(dd,'19000101',SYSDATETIME()),'19000101');
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 1
    No, date and time functions are database specific. Whether or not `now()` is valid all depends on their database type (which they did not specify). I suspect you meant **MS SQL** rather than SQL. – Leigh Jun 17 '12 at 06:17
  • @Leigh I did mean MS SQL- for what it's worth (-1!) I'll edit my post – whytheq Jun 17 '12 at 17:25
  • Thanks for correcting it. For some reason SO's not letting me reverse the -1 and locked my vote. Can you try updating the post again? – Leigh Jun 18 '12 at 01:05