You are here: Re: How to create sql statement with php to insert null in database with update « PHP SQL « IT news, forums, messages
Re: How to create sql statement with php to insert null in database with update

Posted by ZeldorBlat on 01/22/07 03:39

JM wrote:
> When you pass an array to a function that will update a record in a
> database.
>
> for example:
> function doIt($file) {
> //connect to database
>
> $sql = 'UPDATE Files ' .
> "SET FileName = '" . $file['FileName']. "', " .
> // what if $file['Author'] or $file['Description']
> // equals null or ''? Should I test for this and construct
> // my sql statement as a result of several if statements ?
> // or is there another possibility ?
> // FileID and FileName cannot be null so this is easy,
> // but what for the other fields
> "WHERE FileID = '" . $file['FileID'] . "'"
> // execute sql
> // return result
> }
>
> JM

You've already described the answer. You "should...test for this and
construct my sql statement as a result...". But you don't need a bunch
of "if" statements to do it. Use the ternary operator as a shortcut:

$sql = 'UPDATE Files ' .
"SET FileName = '" . $file['FileName']. "', " .
"Author = " . (empty($file['Author']) ? 'null' : "'" .
$file['Author']) . "',".
"Description = " . (empty($file['Description']) ? 'null' :
"'" . $file['Description']) . "' " .
" WHERE FileID = '" . $file['FileID'] . "'"

For more info see this:

<http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary>

 

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

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