Re: Inserting a NULL value into MySQL via PHP
| From: | Curt Zirzow | Date: | Fri, 11 Nov 2005 05:15:44 +0000 |
| Subject: | Re: Inserting a NULL value into MySQL via PHP | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-225597@lists.php.net to get a copy of this message | ||
On Fri, Nov 11, 2005 at 01:09:39PM +1300, Jasper Bryant-Greene wrote:
> benc11@gmail.com 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.
--