Re: Simple Question: PHP, MySQL, HTML Form and NULL
| From: | Fred | Date: | Wed, 05 Dec 2001 00:39:59 +0000 |
| Subject: | Re: Simple Question: PHP, MySQL, HTML Form and NULL | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-76626@lists.php.net to get a copy of this message | ||
Your form returns the date as an empty string "" which is an invalid date.
MySQL sets all invalid dates to 0000-00-00. There is a big difference
between an empty string and NULL. If you want to insert NULL into the MySQL
date field you will need to insert "/0" which is the escape sequence for
NULL.
In the script that inserts the form data just add something like:
if ($Date == "") $Date = "/0";
somewhere before the insert.
Fred
<sgibbs@vt.globalhealth.org> wrote in message
news:BB6D932A42D6D211B4AC0090274EBB1DA0F139@GLOBAL1...
> I have added a new column in an existing MYSQL table called
event_date" -
> type: DATE, NULL default.
>
> This database field will be filled if and when the field in the HTML form
is
> complete. When I added the new field to MySQL, it automatically assigned
> NULL to all the existing records - which is what I wanted it to do.
>
> I tested the HTML form, and when someone enters a date in the HTML form,
the
> date appears correctly in the table field. Perfect.
>
> Now my question: When the HTML form date field is left blank and the form
> is submitted, instead of putting "NULL" in the MySQL event_date field, I
> found: 0000-00-00. Is this because the form is submitting an " " to the
> database field?
>
> Is there some kind of if/then statement I should use so that when the
field
> is empty, "NULL" will be entered into that table field?
>
> Thank you, Shawna