Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Saving an empty date from PHP into a MySQL DATETIME column fails

ErrorThe insert fails with an error about an invalid datetime value when the date field is empty

Short answer

An empty string is not NULL. In MySQL's default strict mode a '' in a DATETIME column is an invalid value and the insert fails. The column must also allow NULL. Change the column if needed, and send the SQL value NULL when the form field is empty.

  • Applies to MySQL 8.4, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the column accepts NULL, and what SQL mode the server uses.

    SHOW COLUMNS FROM my_table LIKE 'published_at';
    SELECT @@sql_mode;
  2. 2Match what you see, then jump to its fix

    1. 1SHOW COLUMNS shows NO in the Null columnThe column is declared NOT NULLFix
    2. 2The query shows '' where the date should bePHP sends an empty string instead of NULLFix
    3. 3The error mentions '0000-00-00 00:00:00'A zero date is rejected in strict modeFix
    4. 4A query seems to prove that no NULL values existLooking for NULL rows with = NULLFix

What does the error look like?

  • The insert fails with an error about an invalid datetime value when the date field is empty
  • The column holds 0000-00-00 00:00:00 instead of NULL

Cause 1 of 4

The column is declared NOT NULL

1Check

In strict mode, a missing value for a NOT NULL column with no default is an error. For a NULL column, NULL is inserted when the value is missing.

SHOW COLUMNS FROM my_table LIKE 'published_at';

2Fix

Redefine the column so it allows NULL. MODIFY replaces the whole definition, so include every attribute the column should keep.

ALTER TABLE my_table MODIFY published_at DATETIME NULL DEFAULT NULL;

3Verify

Insert a row with NULL.

INSERT INTO my_table (published_at) VALUES (NULL);

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

PHP sends an empty string instead of NULL

1Check

NULL and the empty string are different values. Strict mode treats a value of the wrong type for the column as invalid, and with strict mode off an invalid DATETIME turns into the zero value.

SELECT published_at IS NULL, published_at = '' FROM my_table LIMIT 5;

2Fix

Turn an empty field into the SQL keyword NULL. A SQL-side way is NULLIF, which returns NULL when both arguments are equal.

-- in the statement; :d is your bound value
INSERT INTO my_table (published_at) VALUES (NULLIF(:d, ''));

3Verify

The stored value should read back as NULL.

SELECT id FROM my_table WHERE published_at IS NULL;

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 4

A zero date is rejected in strict mode

1Check

MySQL 8.4's default SQL mode includes STRICT_TRANS_TABLES and NO_ZERO_DATE. With both on, '0000-00-00' is not permitted and an insert produces an error.

SELECT @@sql_mode;

2Fix

Do not store zero dates. Store NULL for unknown dates. Do not turn off strict mode just to make the error go away.

UPDATE my_table SET published_at = NULL WHERE published_at = '0000-00-00 00:00:00';

3Verify

Run the previously failing insert again.

SELECT COUNT(*) FROM my_table WHERE published_at IS NULL;

Confirmed by dev.mysql.com

Did this fix it?

Cause 4 of 4

Looking for NULL rows with = NULL

1Check

expr = NULL is never true. Only IS NULL finds NULL values, and = '' finds an empty string.

SELECT * FROM my_table WHERE published_at = NULL;

2Fix

Use IS NULL or IS NOT NULL.

SELECT * FROM my_table WHERE published_at IS NULL;

3Verify

The rows you inserted with NULL are listed.

SELECT COUNT(*) FROM my_table WHERE published_at IS NULL;

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Decide per column whether it is NULL or NOT NULL and use a prepared statement that can send NULL.
  • Keep strict mode on; fix data instead of relaxing the server.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

Laravel bulk insert fails with 'too many placeholders'

SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders

Laravel sends one placeholder per value, so one insert of many rows times many columns, or a whereIn with a huge list, creates a statement MySQL refuses.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.