On the table creation, set the default on the timestamp column to CURRENT_TIMESTAMP.
CREATE TABLE IF NOT EXISTS messages (
mess_id int PRIMARY KEY AUTO_INCREMENT,
mess_from int,
mess_to int,
mess_txt VARCHAR(20000),
mess_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (mess_from) REFERENCES users(id),FOREIGN KEY (mess_to) REFERENCES users(id) )
EDIT:
For completeness, this will set the mess_time field to the current timestamp when a new record is inserted and does not contain an explicit value for that field. It will have no effect when an UPDATE on that row occurs. You can set it so that on every update the timestamp will be set to the current timestamp using "ON UPDATE CURRENT_TIMESTAMP"
CREATE TABLE IF NOT EXISTS messages (
mess_id int PRIMARY KEY AUTO_INCREMENT,
mess_from int,
mess_to int,
mess_txt VARCHAR(20000),
mess_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (mess_from) REFERENCES users(id),FOREIGN KEY (mess_to) REFERENCES users(id) )
When doing the INSERT, do not specify the timestamp column:
INSERT INTO user_messages (mess_from,mess_to, mess_text) VALUES (@FromValue,@ToValue,@TextValue)
(The above will work in all cases. There is a system variable that controls whether you can use NULL to insert the current_timestamp. This is not standard and less compatible with other SQL engines, so it is often set to standards mode.
The variable is explicit_defaults_for_timestamp, but it has been deprecated, and these non-standard behaviors are set to be removed in future mysql versions, so I would not recommend using it.)