1

I have searched far and wide for a specific case were i could get this answer but what i have seems to me like it should work. but it doesn't.

I basically want to concatenate data into a row in my database. Put simply if there is no existing data it injects the filenames variable into the row.

If there is existing data in the row i need to append a "," to the start so that i can break the filenames out later with php on my page.

Here's what i have that doesn't seem to work.

  $query  = "UPDATE plan 
             SET plan_files = if(plan_files is null,concat(plan_files,
                   '{$filenames}'),concat(plan_files, ',{$filenames}')) 
             WHERE plan_id='{$planid}'";

2 Answers 2

3

try this

$query = "update plan  SET plan_files = (case when plan_files IS NULL then '".$filenames."' else concat(plan_files , ',".$filenames."') end ) where plan_id =". $planid;

Please make sure that $filenames is string and $planid is int.

Sign up to request clarification or add additional context in comments.

3 Comments

Tryed the query and it added the "," even tho there was no data inside the row.
plz check the data type of the column plan_files. it may not be allowed null, or default null. please do these two thing. then i am sure it will work.
Thanks Bilal Works a treat, wasn't savvy on that setting on my table row. Thanks
2

You can do like this :

    "UPDATE `plan` SET 
     plan_files=CASE
     WHEN (plan_files='') THEN  '".$filenames."'
     ELSE concat(name,',".$filenames."')
     END
     WHERE plan_id='".$planid."'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.