1

i have a table called entry .

Table have 2 column called id & name .

When user filled the name and submit name is saved in entry table .

Now i want to save the date also . But this date need to filled automatically in database . That is when name is filled the current timezone of my selected country should be saved automatically in the table .

I already created the date column in table . Now how can i do this ? Is there any stored procedure will help this ?

I cant write any query in my php file . It should done in mysql side .

Please help

1

2 Answers 2

1

You may use trigger for same. Use after insert so whenever you add a record it will automatically update date.

I am not a regular mysql user, so syntax might be different but you'll get the idea for same. Please find the link for more info. TRIGGER.

CREATE TRIGGER inserttable 
AFTER INSERT 
ON table1 
FOR EACH ROW 
   NEW.DATECOL = (SELECT CURDATE());
END

Feel free to edit this code if any syntax issue is there.

Sign up to request clarification or add additional context in comments.

Comments

1

In general, the creation date/time can be handled through the table definition using a default value:

create table t (
    . . . , 
    created_at timestamp default current_timestamp
);

This is much simpler than defining a trigger for this purpose.

Note that this stores the date and time value. I don't think MySQL lets you store only the time. But because you mention timezones, this may be what you really want.

1 Comment

but the table is already created before and there are some entries in table . For the previous entry the date will be like 00.00.000 like that . So please check . I need to store only date , even if time is zero

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.