Re: [PHP] Inserting a NULL value into MySQL via PHP
I tried this and it didn't work using PHP it just leaves a blank instead of a NULL setting. Any other ideas? On 11/11/05, Richard Lynch [EMAIL PROTECTED] wrote: On Thu, November 10, 2005 11:15 pm, Curt Zirzow wrote: ?php $sql_quoted = array(); // shiflett' -- style $myFieldValue = isset($POST['myFieldValue'])? $_POST['myFieldValue']: ''; if (strlen(trim($myFieldValue)) { $sql_quoted['myField'] = ' . mysql_real_escape_string($myFieldValue) . '; } else { $sql_quoted['myField'] = 'NULL'; } I personally would do this part all in one shot: $field = (isset($_CLEAN['field']) strlen($_CLEAN['field'])) ? '$_CLEAN[field] : 'NULL'; Otherwise, I find myself too distracted by all the assignments and if/else logic, and too likely to mess them up later with code changes in earlier/later lines. Note that you already have the apostrophes in $field for non-NULL, so you would just do: $query = insert into foo (field) values($_CLEAN[field]); with no apostrophes $_CLEAN represents an escaped and filtered string, or an unset index, if nothing was in $_POST to start with. Or you can just use the empty string '' in $_CLEAN if you find that easier to process. More than one way to skin a cat. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- ** The content of this e-mail message and any attachments are confidential and may be legally privileged, intended solely for the addressee. If you are not the intended recipient, be advised that any use, dissemination, distribution, or copying of this e-mail is strictly prohibited. If you receive this message in error, please notify the sender immediately by reply email and destroy the message and its attachments. *
Re: [PHP] Inserting a NULL value into MySQL via PHP
On Thu, November 10, 2005 11:15 pm, Curt Zirzow wrote: ?php $sql_quoted = array(); // shiflett' -- style $myFieldValue = isset($POST['myFieldValue'])? $_POST['myFieldValue']: ''; if (strlen(trim($myFieldValue)) { $sql_quoted['myField'] = ' . mysql_real_escape_string($myFieldValue) . '; } else { $sql_quoted['myField'] = 'NULL'; } I personally would do this part all in one shot: $field = (isset($_CLEAN['field']) strlen($_CLEAN['field'])) ? '$_CLEAN[field] : 'NULL'; Otherwise, I find myself too distracted by all the assignments and if/else logic, and too likely to mess them up later with code changes in earlier/later lines. Note that you already have the apostrophes in $field for non-NULL, so you would just do: $query = insert into foo (field) values($_CLEAN[field]); with no apostrophes $_CLEAN represents an escaped and filtered string, or an unset index, if nothing was in $_POST to start with. Or you can just use the empty string '' in $_CLEAN if you find that easier to process. More than one way to skin a cat. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Inserting a NULL value into MySQL via PHP
Is there a way when making a MySQL database entry through a PHP script and there is no data to make the db treat it as NULL?
Re: [PHP] Inserting a NULL value into MySQL via PHP
[EMAIL PROTECTED] wrote: Is there a way when making a MySQL database entry through a PHP script and there is no data to make the db treat it as NULL? Wouldn't this just work: INSERT INTO myTable (myField) VALUES (NULL) Jasper -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Inserting a NULL value into MySQL via PHP
On Fri, Nov 11, 2005 at 01:09:39PM +1300, Jasper Bryant-Greene wrote: [EMAIL PROTECTED] wrote: Is there a way when making a MySQL database entry through a PHP script and there is no data to make the db treat it as NULL? Wouldn't this just work: INSERT INTO myTable (myField) VALUES (NULL) yeah, the final result would need to look like that. Without an example i would guess the question would be more how do i get my statement to send NULL instead of ''. ?php $sql_quoted = array(); // shiflett' -- style $myFieldValue = isset($POST['myFieldValue'])? $_POST['myFieldValue']: ''; if (strlen(trim($myFieldValue)) { $sql_quoted['myField'] = ' . mysql_real_escape_string($myFieldValue) . '; } else { $sql_quoted['myField'] = 'NULL'; } $query = INSERT INTO myTable(myField) VALUES({$sql_quoted['myField']}); echo $query; ? And if the field posted was empty, it will be indentical to Jasper's sql, other wise it will be a properly quoted string. Curt. -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php