Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:39+00:00 2026-05-29T06:12:39+00:00

I have a DATETIME column(Default is NULL) in MySQL and trying to insert Empty

  • 0

I have a DATETIME column(Default is NULL) in MySQL and trying to insert Empty or NULL value. But I am getting “Incorrect datetime value: ” ” error message.
If I insert ‘NULL’ then I am getting “Incorrect datetime value: ‘NULL'” error message. How can I insert a blank or NULL value in this column? Thank you for any suggestions.

Here is the code.

        if (empty($_POST["date_field"]))
        {      
          $Date1 = 'NULL';
        }
        else
        {
           $Date1 = strtotime($_POST["date_field"]);
           $Date1 = date("Y-m-d H:i:s", $Date1);
        }

   INSERT INTO Table1(date_field) VALUES('" .$Date1. "');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:40+00:00Added an answer on May 29, 2026 at 6:12 am

    Your problem here is that you’re inserting 'NULL' surrounded in quotes, which makes it a string. Instead you need the bare NULL

     if (empty($_POST["date_field"]))
     {      
       $Date1 = NULL;
     }
     else
     {
       $Date1 = strtotime($_POST["date_field"]);
       $Date1 = date("Y-m-d H:i:s", $Date1);
     }
    
    // Surround it in quotes if it isn't NULL.
    if ($Date1 === NULL) {
      // Make a string NULL with no extra quotes
      $Date1 = 'NULL';
    }
    // For non-null values, surround the existing value in quotes...
    else $Date1 = "'$Date1'";
    
    // Later, inside your query don't use any additional quotes since you've already quoted it...
    INSERT INTO Table1(date_field) VALUES($Date1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column created as `date_start` datetime NOT NULL DEFAULT '1970-01-01' However when
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
Suppose I have a datetime column in MySQL. How do I select all that
I have a Mysql datetime column, what should be the corresponding datatype in a
I have a DateTime which I want to store in a Date MySQL column.
In mysql database i have this column called: Name: Date Type: datetime I have
I have a table with this column: last_modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON
Let's assume I have a table with a CreatedBy Datetime column with a default
At present I have a column with datatype datetime with default constraint. Now I
I have a datetime column in db that I want to transform into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.