-1

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?

ewef
  • 57
  • 8

2 Answers2

0

You need to have NULL as string:

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

when PHP replaces variable in a query string - $date_birth is replaced by empty string, while you need a string NULL

Distdev
  • 2,312
  • 16
  • 23
0

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)";
Richard
  • 618
  • 1
  • 9
  • 15