12

trying to add text to an existing value in mysql using mysql's concat function?

so existing value is currently 'john' and after function has run will be 'johnewvalue'

i am trying to add a piece of text ':reply' to the existing text which is in my subject column in my database ptb_messages.

i am trying to do this using mysql's concat function but im not getting any result what so ever.

$sql = mysql_query("UPDATE ptb_messages SET subject = CONCAT subject, 'newvalue' WHERE id='".$message_id."'"); 

can someone please show me a way of getting it to do what i want. thanks.

1

1 Answer 1

31

it should be

UPDATE ptb_messages 
SET subject = CONCAT( subject, 'newvalue')
WHERE ... 

in PHP

$sql = mysql_query("UPDATE ptb_messages SET subject = CONCAT(subject, 'newvalue') WHERE id='".$message_id."'"); 

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

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

7 Comments

what do you mean by run only once?
well when the user replys to a users message this puts :reply after the subject heading, but if :reply exists then leave subject as is and do not add :reply any more times.
uhm.. can you give sample records with desired result?
example: subject 'test' when submitted becomes 'test:reply' then if user submits again where message_id = '' then subject DOES NOT update to 'test:reply:reply' instead it stays as 'test:reply'
i get your point now but i'm still confuse. how will you know that the last concatenated value is equal to the current subject, well give this a try, UPDATE ptb_messages SET subject = IF(subject LIKE '%newValue', '', CONCAT(subject, 'newvalue')) WHERE ....
|

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.