D
D
Dmitry Kim2015-09-15 08:56:43
MySQL
Dmitry Kim, 2015-09-15 08:56:43

Who can explain the "weird" behavior in MySQL and help fix the error?

The table has a field of type DATETIME and it can be NULL.
When the user does not fill in the corresponding field in the HTML form, naturally its value will be an empty string. The following query is made like:

UPDATE `tbl` SET ...bla bla bla... `d` = DATE(:date)

it does not pass, an error occurs Error Code: 1292. Incorrect datetime value: ''
Although the query returns NULL without error
SELECT DATE('')
as expected .

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Kim, 2015-09-15
@kimono

Replaced with:

UPDATE `tbl` SET ...bla bla bla... `d` = IF(:date, DATE(:date), NULL)

D
Dmitry Entelis, 2015-09-15
@DmitriyEntelis

So null and "" are 2 different things.

O
Optimus, 2015-09-15
Pyan @marrk2

Check the value before inserting, and if it's empty explicitly set $date=0; `d`='".$date."'

G
Gregory, 2015-09-15
@grigruss

<?php ........`d`='".(($date=="")?"0000-00-00 00:00:00":$date)."'... ... ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question