Skip to content
Advertisement

Inserting date in null [closed]

I am inserting date. It works when date is set, otherwise, it doesn’t work.

DateBirth type is date and its default value is NULL.

Here is my php

$date_birth = isset($_POST['date_birth']) ? $_POST['date_birth'] : NULL;

Here is mysql

$adding_birth = "INSERT INTO person (DateBirth) VALUES ('$date_birth')";

How can I get date field in null?

Advertisement

Answer

You are currently adding NULL as an STRING to your database. You have to rewrite your SQL command to:

$date_birth = isset($_POST['date_birth']) ? "'" . $_POST['date_birth'] . "'" : 'NULL';
$adding_birth = "INSERT INTO person (DateBirth) VALUES ($date_birth)";

When you do an echo in front of the SQL you will get, with the NULL in your case:

$adding_birth = "INSERT INTO person (DateBirth) VALUES ('NULL')";

And correct is:

$adding_birth = "INSERT INTO person (DateBirth) VALUES (NULL)";
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement