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