You are here: Re: [PHP] Inserting a NULL value into MySQL via PHP « PHP « IT news, forums, messages
Re: [PHP] Inserting a NULL value into MySQL via PHP

Posted by Curt Zirzow on 11/11/05 07:15

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.
--

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация