简体   繁体   中英

Failing to insert timestamp into MySQL database

i have a form where user insert info such as name/address etc. there is a timestamp of when the entry is created and modified. the "modified" field used to just default to 0000-00-00 but lately its been causing the error

Array
(
    [0] => Invalid query: Field 'modified' doesn't have a default value
)

i looked around and someone suggested setting the modified column to 'NULL' but the database doesn't allow that

i suspect the problem is that when user clicks submit, it doesn't submit anything into the 'modified' column because the 2nd error message i get after the one above is

INSERT INTO stores (name,cat_id,address,telephone,email,website,description,embed_video,default_media,latitude,longitude,created) VALUES('bsg',',29,','4750 abc dr','11212121212','abc@gmail.com','','drrrr','','image','44.7847745','-93.19667930000003','2019-06-13 15:28:39')

notice that the 'modified' attribute isn't listed in one of the parameter being inserted. I'm guessing that's the problem, but idk where to fix that.

i checked my settings.php, validate.php, config.inc.php but nothing i see so far looks like it would fix the issue

There are a number of ways to fix your issue, some of them better / more efficient than this, however this one matches closely with the information you have provided in your question.

In your database Configure the modified column so that:

  • It accepts NULL as a value. (ie Unset NOT-NULL , if this has been set previously)
  • (Optionally) add a default value to the column of your choosing 0000-00-00

(Optionally) in your insert statement, within your code:

  • Explicitly specify a value for the modified column of NULL or 0000-00-00

Explaining Alex's answer and using some code examples, you might want to have the field accepting null values:

ALTER TABLE `stores` CHANGE `modified` `modified` DATE  NULL;

Or just have a default value of '0000-00-00' (which is the previous behavior and a really bad thing to do and might cause future problems when stricter modes are activated in MySQL)

ALTER TABLE `stores` CHANGE `modified` `modified` DATE  NOT NULL  DEFAULT '0000-00-00';

Or have MySQL use the current date when modified (that would be my choice) without even changing anything in the mysql structure:

INSERT INTO stores (name,cat_id,address,telephone,email,website,description,embed_video,default_media,latitude,longitude,created,modified) VALUES('bsg',',29,','4750 abc dr','11212121212','abc@gmail.com','','drrrr','','image','44.7847745','-93.19667930000003','2019-06-13 15:28:39',CURRENT_DATE);

Or even your desired '0000-00-00' value:

INSERT INTO stores (name,cat_id,address,telephone,email,website,description,embed_video,default_media,latitude,longitude,created,modified) VALUES('bsg',',29,','4750 abc dr','11212121212','abc@gmail.com','','drrrr','','image','44.7847745','-93.19667930000003','2019-06-13 15:28:39','0000-00-00');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM