PDA

View Full Version : added field with time & date stamp


awayne96
10-18-2009, 11:50 PM
I have a field that is call added that has a time & date stamp but I also have a updated field aswell that only has data entered once the data has been updated.

I am trying to have the search results either show the added time&date stamp but each time you update it have a new date entered into that field of the database... Of can I use the updated field and have data entered into that field when it is originally posted to the database?

I'm thinking I can solve this by changing the attributes for the added field to "on update current_timestamp" and each time the data is updated it would have new date & time entered into the added field? Am I right about what I am thinking or do I need to add code to my modify page to update with new time & date info to either field?

Thanks for your help.

Old Pedant
10-19-2009, 05:38 AM
You would need a TRIGGER to have the value changed any time the record is modified.

I would think it would be easier to use update the field at the same time you modify any other fields.

awayne96
10-19-2009, 07:23 AM
Is there a way to update the date/timestamp field in the table hidden within my form? Since this field is set automatically when the user submits the original form of data.

Thanks

Old Pedant
10-19-2009, 08:48 AM
Ummm...you just put it into the server side code. Don't need anything in the <form>.

Say you are updating the phone number:

$sql = "UPDATE userinfo "
. " SET phone = '" . $_POST["phone"] . "', lastupdate=NOW() "
. " WHERE userid = " . $_POST["userid"];

Or similar. (I'm not a PHP person, so ignore PHP typos, please.)

Assuming that your field is named lastupdate, then just assigning the MySQL *built in function* NOW() to it does the job.

The MySQL manual is your friend. Try to at least bookmark the important pages.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now