Date field preclude with the current date automatically

5

How do I set up mysql in phpmyadmin so that when I insert a record the data_lancamento field automatically pops up with the current date without me needing to be passing the date through php, does anyone know?

  

I saw a question just like my here / I did not get an answer   significant

    
asked by anonymous 18.06.2014 / 17:54

1 answer

3

Set data_lancamento to type TIMESTAMP , and set to Default CURRENT_TIMESTAMP .

Creating an 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 keep datetime or date just put now() in your SQL like this:

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: For Date or DateTime there is no same resource of type TIMESTAMP .

References:

18.06.2014 / 18:22