Jump to content

introducing null value


carrilo

Recommended Posts

I want to introduce to  a null value introduced into a table when I don't introduce any value.

 

I have the following code which I thought would do this but it in fact introduces a text value of "null" if I leave the quotation marks off it introduces a blank value (not null). How can I introduce a real null value?

$al3 = ( !empty($_POST['al3']) ) ? ($_POST['al3']): "NULL";
Link to comment
Share on other sites

PHP's idea of NULL is not necessarily SQL's idea of NULL.

 

If you want to insert NULL then the text NULL must appear in the query:

 

INSERT INTO table (columnname) VALUES (NULL);

 

This means that if your query puts quotes around it it will fail. If you append a variable containing NULL to a quer string you'll get a bad query.

If you give it an empty string it will store an empty string.

 

You'd have to use a construction like NULLIF(), but that only works if you make a distinction between NULL and an empty string.

 

So what remains is the prepared statement: http://php.net/manual/en/mysqli.prepare.php to which you can feed a variable with value NULL (not the string, but the PHP value (wel, lack of value, NULL indicates that the value is not known))

Link to comment
Share on other sites

The correct answer has already been posted:

$al3 = ( !empty($_POST['al3']) ) ? ($_POST['al3']): null;

Make sure that the relevant field excepts null values.

If you are not sure, use the "Explain table" syntax and post it here.

 

Also, never insert user data directly into a mysql query. Always properly sanitize the data first.

Link to comment
Share on other sites

[quote

The correct answer has already been posted:

$al3 = ( !empty($_POST['al3']) ) ? ($_POST['al3']): null;

 

Don't use empty() for this, you don't want the value zero or an empty string to be converted to null. Small difference, huge consequences.

 

 

@carillo, what do not not understand about prepared statements?

Link to comment
Share on other sites

My mistake, this will not work unless null is specified upon insert time since PHP treats null values differently then MYSQL.

You can however do this using a prepared statement in MYSQLi or PDO which should be what you are using anyway.

 

Below is pseudo code to illustrate an example usage:

$stmt = $mysqli->prepare("INSERT INTO table (field1, field2) VALUES (?, ?)");
$stmt->bind_param('ss', $field1, $field2);

$field1 = "string1";
$field2 = null;

$stmt->execute();
Edited by DFulg
Link to comment
Share on other sites

this works with

 

 

if you append $a13 into the query then yes, because you are creating the word NULL in SQL.

 

Still don't use empty() because that's not what you need, or should want. (basically just forget that empty() exists, it is quite a pointless function)

Edited by vinny42
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.