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.
In syntax, First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.
To prepend a string to a column value in MySQL, we can use the function CONCAT. The CONCAT function can be used with UPDATE statement.
Which MySQL function can be used to add text to an existing value? We can append a string of data to an existing data of a field by using concat function of MySQL.
CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With