Jump to content

Building a SQL query - unexpected T_IF


NathanLedet

Recommended Posts

I'm building a little animal management system where people can update records of animals they keep.

 

I have given the ability to upload an image - and when a user edits an animal, and chooses NOT to upload a new image, then the image initially chosen will not change.

 

So, in my code to process the submission, I have the following SQL query that is running into an unexpected T_IF.

 

    	$sql = "UPDATE rcms SET 
    	`animal_id` = '{$animalid}', 
    	`type` = '{$animaltype}',
    	`weight` = '{$animalweight}',
    	`birthday` = '{$animalbday}',
    	`description` = '{$animaldescription}'"
    	
    	. if ($filename != NULL){    	
    	", `image` = '{$filename}'"
    	}.
    	
    	"WHERE id='{$id}' LIMIT 1";

 

I have other code that process the image if it's there, or sets the $filename variable to NULL if the file field is left blank.

 

So, essentially, All I'm trying to do is say if $filename is not NULL (has a name of a new file) - add that snippet of code so that it will update in the database.

Link to comment
https://forums.phpfreaks.com/topic/155866-building-a-sql-query-unexpected-t_if/
Share on other sites

You can't use concatenation like this.

 

Try:

$sql = "UPDATE rcms SET
       `animal_id` = '{$animalid}',
       `type` = '{$animaltype}',
       `weight` = '{$animalweight}',
       `birthday` = '{$animalbday}',
       `description` = '{$animaldescription}'"
       .(($filename != NULL) ? ", `image` = '{$filename}'" : "").
       "WHERE id='{$id}' LIMIT 1";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.