Re: Inserting a NULL value into MySQL via PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Nov 11, 2005 at 01:09:39PM +1300, Jasper Bryant-Greene wrote:
> benc11@xxxxxxxxx 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux