Skip to content
Advertisement

update datetime with null value in mysql by php

I’m using this code to update my datatime field to NULL, php version 7.3.7

JavaScript

it gives error when $timestamp is null:

Incorrect datetime value: ” for column farsifor_m.forms.enddate at row 1

But when I quote “NULL” and remove quotations around $timestamp in the query, if $timestamp is null it works properly but if $timestamp value is not null it gives error.

JavaScript

Advertisement

Answer

Learn to use prepared statements; and do not inject post variables to “build” the query. Having said that:

You can use NULLIF for convenience. In the following example the specific value 0000-00-00 00:00:00 will be converted to NULL:

JavaScript

Or you can simply:

JavaScript

And use PHP to pass a variable containing string or null.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement