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

  • SEARCH
  • Home
  • 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 8533079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:52:24+00:00 2026-06-11T09:52:24+00:00

I’m having a helluva time figuring this one out – it seems to be

  • 0

I’m having a helluva time figuring this one out – it seems to be a well documented question, but none of the solutions go quite far enough to completely solve my problem.

Basically I’ve got a database for our website that saves upcoming events; occasionally these events have more than one showing, so we have fields where we can put in these dates if it does. Most of the time, there’s just one date to put in, so our extra date fields (date2, date 3, etc) are blank. If they’re blank, I want them to be saved to the mysql database as NULL.

An example of what I’ve got for tossing the values to mySQL looks like this:

if (empty($_POST['date2']))
        {$date2 = NULL;
        }
    else
        {$date2 = mysql_real_escape_string(date("Y-m-d",strtotime($_POST['date2'])));
        }

Which is doing what it’s supposed to. The problem comes when we try to update the variable as follows:

    mysql_query("UPDATE events
             SET date2='".mysql_real_escape_string($date2))."',

mySQL doesn’t know what this value is (obviously, because you can’t escape string or format a date on NULL) so it saves the value as 0000-00-00 instead (or 1969-12-31…depending on my attempt to fix the problem). What I can’t figure out is how to input a value for date2 without using ”s.

I tried changing it to date2 = $date2, But the code won’t execute and tosses up an error. date2=’$date2′, executes – but again it’s not saving NULL correctly (I think because it’s saving ‘NULL’ and not NULL?).

Any advice?

  • 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-06-11T09:52:26+00:00Added an answer on June 11, 2026 at 9:52 am

    You have the variable in single quotes:

    date2='".mysql_real_escape_string($date2))."',
    

    Which means you are trying to save a string to the field.

    If you want it to be null you need to drop the quotes around it.

    insert into YourTable ('null'); // Will try to insert a string
    insert into YourTable (null); // Will try to insert a null
    

    Edit: Why not pop it directly into the if statement right off the bat?

    if (empty($_POST['date2']))
    {
        $date2 = NULL;
        mysql_query("UPDATE events SET date2=null, ...";
        // This will also work...
        mysql_query("UPDATE events SET date2=".$date2.", ...";
    }
    else
    {
        $date2 = mysql_real_escape_string(date("Y-m-d",strtotime($_POST['date2'])));
        mysql_query("UPDATE events SET date2='".mysql_real_escape_string($date2))."', ...";
    }
    

    Updated code:

    $query="update events set ";
    
    if (empty($_POST['date2']))
    {
        $date2 = NULL;
        $query.="date2=null, ";
    }
    else
    {
        $date2 = mysql_real_escape_string(date("Y-m-d",strtotime($_POST['date2'])));
        $query.= " date2='".mysql_real_escape_string($date2)."', ";
    }
    
    // a few other if statements to finish up the rest of your fields...
    
    $query.=" where someCondition=SomeOtherCondition";
    mysql_query($query);
    

    On that note, if you are writing this code nice and fresh, you might want to consider starting with PDO. and using prepared statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.