简体   繁体   English

无法将时间戳插入MySQL数据库

[英]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 “modified”字段用于默认为0000-00-00但最近导致错误

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 我环顾四周,有人建议将修改后的列设置为“NULL”,但数据库不允许这样做

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. 请注意,'modified'属性未在其中一个参数中列出。 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 我检查了我的settings.php,validate.php,config.inc.php,但到目前为止我看不到它会解决问题

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: 在数据库中配置modified列,以便:

  • It accepts NULL as a value. 它接受NULL作为值。 (ie Unset NOT-NULL , if this has been set previously) (即,如果先前已设置,则取消设置NOT-NULL
  • (Optionally) add a default value to the column of your choosing 0000-00-00 (可选)将默认值添加到您选择的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 明确指定NULL0000-00-00modified列的NULL

Explaining Alex's answer and using some code examples, you might want to have the field accepting null values: 解释Alex的答案并使用一些代码示例,您可能希望该字段接受空值:

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) 或者只是有一个默认值'0000-00-00'(这是以前的行为,这是一件非常糟糕的事情,在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: 或者让MySQL在修改时使用当前日期(这将是我的选择),甚至不更改mysql结构中的任何内容:

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: 甚至是你想要的'0000-00-00'价值:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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