Zakman411
Zakman411

Reputation: 1764

SQL: Updating using timestamp

I have a table with a column called 'updated'. When a new row is CREATED, the column automatically inserts the current time. However, I'd like to UPDATE the column as well..any input on how to do this? Here's my update statement (doesn't have the 'updated' column yet):

$update = mysql_query("UPDATE documents SET company = '$company',claimnumber = '$claimnumber',fullname = '$fullname',dateofloss = '$dateofloss',foruser = '$foruser' "."WHERE doc_id =".$doc_id);

Upvotes: 1

Views: 1226

Answers (3)

Paul Walls
Paul Walls

Reputation: 6044

ALTER TABLE `documents` 
ADD COLUMN `UpdatedDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP NOT NULL;

Edit: To change...

ALTER TABLE `documents` 
MODIFY `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP NOT NULL;

Upvotes: 1

mu is too short
mu is too short

Reputation: 434615

Use a trigger, something like this:

create trigger updated_is_now before update on documents
for each row set NEW.updated = now();

Then you can send in your usual SQL UPDATE statements and the trigger will, effectively, add updated = now() to the SET clause.

Upvotes: 2

alex
alex

Reputation: 490153

If you want the updated column to be updated on update, assign NOW() to it in your query.

Upvotes: 1

Related Questions