Php/Mysql 日期保存为“0000-00-00”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14215331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:52:11  来源:igfitidea点击:

Php/Mysql date saved as '0000-00-00'

phpmysqldate

提问by gniewleone

I have a Mysql database where i keep dates of the clients orders.

Date is generated by a php function date('Y-m-d'). Two records in my database have values '0000-00-00'. One was created by a client from Australia and another by a client from Italy. Other 5000 orders are fine.

My question is how is it possible to generate such date ? Could it be something with individual browser settings ?

Thanks for help

我有一个 Mysql 数据库,用于保存客户订单的日期。

日期由 php 函数生成date('Y-m-d')。我的数据库中的两条记录具有 values '0000-00-00'。一个是由澳大利亚客户创建的,另一个是由意大利客户创建的。其他5000个订单没问题。

我的问题是如何生成这样的日期?可能与个人浏览器设置有关吗?

感谢帮助

回答by Php Geek

The only reason what i can find is either your client has entered improper date format or he has entered an empty string. Remember the date format in Mysql is yyyy-mm-dd.

我能找到的唯一原因是您的客户输入了不正确的日期格式,或者他输入了一个空字符串。记住 Mysql 中的日期格式是 yyyy-mm-dd。

回答by John Conde

It means MySQL was passed an invalid value including no value at all. Check your code to make sure that it always has a valid value for that date.

这意味着 MySQL 被传递了一个无效值,包括根本没有值。检查您的代码以确保它始终具有该日期的有效值。

回答by ThisGuy

I noticed I was having the same exact issue, and it turned out it was simply that I didn't put single quotes around my $date variable when inserting it, thus it was inserting a null or blank value which appeared as 0000-00-00 in the database. Once I put '$date'instead of $datein my insert statement, the value appeared as expected.

我注意到我遇到了同样的问题,结果只是我在插入 $date 变量时没有在它周围加上单引号,因此它插入了一个空值或空白值,显示为 0000-00- 00 在数据库中。一旦我在插入语句中放置了'$date'而不是$date,该值就会按预期出现。

回答by TechSpellBound

Check this post default date '0000-00-00 00:00:00' or null. It will answer your question. Probably, year, month and day variables at their client side code have not been initialized.

检查此帖子默认日期 '0000-00-00 00:00:00' 或 null。它会回答你的问题。可能,他们客户端代码中的年、月和日变量尚未初始化。

EDITIf that's not the case, then it seems you have recently changed the Date-related columnin the database from VARCHAR or TEXTto DATE.

编辑如果不是这种情况,那么您似乎recently changed the Date-related column在数据库中从VARCHAR or TEXTDATE.

MySql changesall NULLvalues in this case to '0000-00-00'. So, if some time back, date has not been a compulsory field on the UI and if it was passed as NULL, then the above thing has happened.

MySql changesNULL在这种情况下的所有值都为'0000-00-00'。所以,如果有一段时间,日期不是 UI 上的强制字段,如果它作为 NULL 传递,那么上面的事情就发生了。

回答by fingerman

It could be the case that you have purely programmed or outdated mysqlfunction. For instance: CURDATE() + 5will insert the date correctly in the DB, however if the user does it on 2016-12-28the DB will show 0000-00-00. It will work fine if it the current date is 2016-12-20. The absolutely correct function would be CURDATE() + INTERVAL 5 DAY. You write at the beginning of Jan, so maybe u have similar issue.

可能是您有纯粹的编程或过时的mysql功能。例如:CURDATE() + 5将在数据库中正确插入日期,但是如果用户2016-12-28在数据库上执行此操作,则会显示0000-00-00. 如果当前日期是 2016-12-20,它将正常工作。绝对正确的函数是CURDATE() + INTERVAL 5 DAY. 你在一月初写的,所以也许你有类似的问题。