Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SQL enters null every 9th month

Tags:

sql

php

Having a sql error 2018-9-31 enters 0000-00-00 into database not too sure why I am getting all 0's only on that date. Any help greatly appreciated, it happens every 3rd quarter insert.

INSERT INTO leagues(start,Name,type,end,cashstock) VALUES( '2018-4-1', 'Quarter 2 2018', 'public', '2018-6-30', '7500' )
INSERT INTO leagues(start,Name,type,end,cashstock) VALUES( '2018-7-1', 'Quarter 3 2018', 'public', '2018-9-31', '7500' )

returns

Edit Edit
 Copy Copy
 Delete Delete
2018-04-01
Quarter 2 2018
3
public
2018-06-30
7500

Edit Edit
 Copy Copy
 Delete Delete
2018-07-01
Quarter 3 2018
4
public
0000-00-00
7500
like image 947
Pete Dawg Avatar asked Apr 27 '26 14:04

Pete Dawg


1 Answers

September only has 30 days. So 2018-9-31 is an invalid date, therefore the default 0000-00-00 is inserted.

I would recommend not leaving this up to chance and either checking valid dates in your application, or letting MySQL handle it by enabling strict mode:

If strict mode is enabled, '0000-00-00' is not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, '0000-00-00' is permitted and inserts produce a warning.

Or you can also use NO_ZERO_DATE:

The NO_ZERO_DATE mode affects whether the server permits '0000-00-00' as a valid date. Its effect also depends on whether strict SQL mode is enabled.

like image 189
ishegg Avatar answered Apr 30 '26 04:04

ishegg