mysql
Carbonbased Lifeform asked:


When a visitor fills out a form, I want to know when the form data was submitted. So how do I make MySQL automatically add a timestamp on the data so that when I pull the data back up, I will have the submitted data in one column and the time the data was submitted in another column?

Comments

J J on 16 August, 2008 at 8:10 pm #

First you need a date/time column called something ingenious like ‘dtInsert’. On your insert statement, append a quick ‘CURDATE()’ for that column, and there you go! Easy fix… assuming you find adding columns to existing tables easy.


nore h on 18 August, 2008 at 6:00 pm #

This might help


Eric V on 21 August, 2008 at 10:12 pm #

Login to MySQL with a user that has sufficient privileges on your database.

select your database by using
use database-name;
add a column to your table using
ALTER TABLE table-name ADD date_entered timestamp DEFAULT CURRENT_TIMESTAMP;

the current_timestamp default will automatically add the date/time when you add a new comment to your database table - you don’t even need to change your insert statement when a comment is submitted!

to retrieve it in a human-readable manner, in your select statement, use DATE_FORMAT( date_entered, GET_FORMAT(DATE, ‘USA’))
That will return the default US version for date, which is usually good enough. If you want to display the date differently, please look at all the possible ways here:

example:
SELECT comment, DATE_FORMAT( date_entered, GET_FORMAT(DATE, ‘USA’)) FROM table-name;

let me know if you need more help


Post a Comment
Name:
Email:
Website:
Comments: