Field date fills with current date automatically

Asked

Viewed 24,335 times

5

How do I configure mysql in phpmyadmin so that when I insert a record the field data_lancamento automatically fill in the current date without me having to pass the date by php, someone knows?

I saw a question like mine here, but I received no reply significant

1 answer

4


Configure the data_lancamento for the guy TIMESTAMP, and put in value Default CURRENT_TIMESTAMP.

Creation of Example Table:

CREATE  TABLE `testdb`.`Exemplo` (    
  `id` INT NOT NULL AUTO_INCREMENT ,    
  `data_lancamento` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,    
  PRIMARY KEY (`id`) 
);

This solution is automatic by the bank itself, but if you do not want to change and maintain the datetime or date just put a now() in your SQL so:

INSERT INTO exemplo (data_lancamento) values (now());

You can also create a Trigger

CREATE TRIGGER `exemplotrigger` BEFORE INSERT ON  `exemplo` 
FOR EACH ROW SET NEW.data_lancamento = NOW();

Note: To Date or DateTime there is not the same resource of type TIMESTAMP.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.