/    Sign up×
Community /Pin to ProfileBookmark

Send NULL variable to MySQL

Basically I am having a lot of trouble figuring out how I can assign NULL to a variable and then send NULL to mysql.

The case is I want to edit some users, and the filed date of birth is a mandatory field, so if they update it then OK, but if they do not it stays NULL.

Now in my edit function I have a varibale that say:

$born_on = $_POST[‘born_on’], which is basically an empty textbox.

when I update mysql it saves that is 00-00-0000 00:00:00

Now even when I put an if condition making $born_on = NULL it still saves the 00-00-0000 00:00:00, how can I change that back to a NULL whenever I make the textbox empty without having to write an extra query just for that field in an if condition.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@tirnaApr 27.2010 — maybe try something like this:

[CODE]
if(empty($_POST['born_on'])) $born_on = null;
[/CODE]


and/or set the value of the DOB column in your sql update query to null.
Copy linkTweet thisAlerts:
@dk_zero-coolApr 27.2010 — #2 Now even when I put an if condition making $born_on = NULL it still saves the 00-00-0000 00:00:00[/QUOTE]

#1 The problem is that you haven't assigned an allow null rule to your column. You need to mark the NULL field for your column to allow a null value.
Copy linkTweet thisAlerts:
@NogDogApr 27.2010 — Don't confuse the PHP [b]null[/b] with the SQL [b]NULL[/b]. When you use the PHP null in a string (such as your SQL string) it will be cast to a zero or empty string (I don't remember which off the top of my head). But to set a column's value to null in SQL, you need to use the NULL keyword in the query. So very simplistically (with no error-checking or SQL injection escaping), you might do something like:
[code=php]
// note use of string listeral "NULL" instead of PHP null...
// ...plus adding single quotes around the actual value if supplied
$born_on = (empty($_POST['born_on'])) ? "NULL" : "'".$_POST['born_on']."'";
$query = "INSERT INTO table_x (born_on) VALUES ($born_on)";
[/code]
Copy linkTweet thisAlerts:
@tirnaApr 27.2010 — 
The case is I want to edit some users, and the filed date of birth is a mandatory field, so if they update it then OK, but if they do not it stays NULL.......[/quote]


When the html form loads, I would display the current DOB in the database as the default value in the form's textbox. This default value is then sent to the server if the user has not changed it.

Then as part of your server side form data validation, why not redirect the user back to the html form if they have entered an invalid DOB in the textbox?
Copy linkTweet thisAlerts:
@XeroSiSauthorApr 28.2010 — Date of birth is not a mandatory field, but I don't want to make different SQL scripts considering cases where date of birth is assigned / date of birth not assigned. Because this is one of many non-mandatory fields basically.

The issue I am facing is that basically I need to somehow make a php variable equal exactly to the MYSQL null basically.

I have yet to try nogdog's approach.
Copy linkTweet thisAlerts:
@tirnaApr 28.2010 — Date of birth is not a mandatory field.....[/quote]

[B]You have now changed what you said in your first post.[/B]

I assume you have set up the column in the db table to allow null values.
Copy linkTweet thisAlerts:
@XeroSiSauthorApr 28.2010 — OH sorry, I meant to say it is not a mandatory field, my apologies for this. Any reply now that you know the case?
Copy linkTweet thisAlerts:
@sonicpaintJul 22.2010 — I have a similar problem and can't seem to get rid of the 0000-00-00 in the database column even event when I've set it to Null.

example: second_date date DEFAULT NULL

When displaying the data, it'll show 2 results..one is Wed Dec 31, 1969 and the other is

Tue Nov 30, 1999.

Can some explain what is going on here?

I've search this in Google and tried a few things with no resolve. If someone could help, I greatly appreciate it.
×

Success!

Help @XeroSiS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...