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
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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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