简体   繁体   English

MySQL STR_TO_DATE()返回无效日期

[英]MySQL STR_TO_DATE() returns invalid date

Our website requires signing up. 我们的网站需要注册。 Lately it has become apparent that the signup process fails on many occasions. 最近很明显,注册过程在许多情况下都失败了。 I implemented several logs in my PHP code to start tracking this. 我在PHP代码中实现了几个日志,以开始对此进行跟踪。 Including one to track outputs from mysql_last_error() when possible. 尽可能包括一个跟踪mysql_last_error()的输出。

My next step was to write a script that generates sign-up requests with random values for the different required fields. 我的下一步是编写一个脚本,为不同的必填字段生成具有随机值的注册请求。 My logic was that if this yields an issue - the problem is with the back-end logic, and if it doesn't - the problem is with server loads. 我的逻辑是,如果这产生了一个问题-问题出在后端逻辑上,如果没有,问题出在服务器负载上。 (BTW, was this sound logic?) (顺便说一句,这听起来合理吗?)

When sending many of these (I'd say about 150) I got only three errors, and the mysql_last_error log showed this: 当发送许多这样的消息时(我会说大约150),我只有三个错误,并且mysql_last_error日志显示如下:

  • [Wed May 21 13:20:45.000000 2014] Incorrect date value: '1964-11-31' for column 'dob' at row 1 [2014年5月21日,星期三13:20:45.000000]错误的日期值:第1行的'dob'列的日期为'1964-11-31'

  • [Wed May 21 13:48:37.000000 2014] Incorrect date value: '1963-11-31' for column 'dob' at row 1 [2014年5月21日,星期三13:48:37.000000]错误的日期值:第1行的'dob'列的日期为'1963-11-31'

  • [Wed May 21 13:48:37.000000 2014] Incorrect date value: '1967-02-29' for column 'dob' at row 1 [2014年5月21日,星期三13:48:37.000000]错误的日期值:第1行的'dob'列的日期为'1967-02-29'

The request is sent in a JSON format, and its dob field is in mm/dd/yyyy format. 该请求以JSON格式发送,其dob字段为mm / dd / yyyy格式。 The SQL in my PHP code subsequently looks like this: 我的PHP代码中的SQL随后如下所示:

"STR_TO_DATE('".$cleanUser['dob']."', '%m/%d/%Y')"

I tried performing this operation inside MySQL Workbench as well, to isolate it from possible bad code. 我也尝试在MySQL Workbench中执行此操作,以将其与可能的错误代码隔离。 but it failed there as well. 但它在那里也失败了。 I simply don't understand what's the problem, and why it occurs only on some dates. 我根本不明白问题出在哪里,为什么它只在某些日期发生。

All help will be greatly appreciated, Thanks in Advance 感谢所有的帮助,在此先感谢

November only has 30 days, so the date 1964-11-31 doesn't exist. 11月只有30天,因此日期1964-11-31不存在。

And 1967 is not a bissextile year, so february only has 28 days this year. 1967年不是双性恋年,所以今年2月只有28天。

BTW, use parameterized query instead of SQL concatenation to avoid leaving your queries open to SQL injection. 顺便说一句,请使用参数化查询而不是SQL串联,以避免使查询对SQL注入开放。

I think error come from data format. 我认为错误来自数据格式。 %m means months in words. %m表示月数。 January, February .etc So could you please use mm or MM format to date 一月,二月.etc等,请问您现在使用的是mm还是MM格式

You can see more details at php manual page http://www.php.net/manual/en/datetime.formats.date.php 您可以在php手册页http://www.php.net/manual/en/datetime.formats.date.php上查看更多详细信息

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

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