-1

I'm building sql UPDATE query to append string value using only NAMED PLACEHOLDERS to the already existing value in db. please suggest necessary changes in below code to work or suggest how to use named placedholders in concat update syntax

$name="Lastname";
$stmt = $conn->prepare("UPDATE users SET name= name + :name WHERE id=:id");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':id', $id);                   
$stmt->execute(); 

Expected Output:

Before: Table has 'name' column-value "Firstname"

After code execution: 'name' column-value "FirstnameLastname"

Sashi
  • 240
  • 4
  • 14
  • Possible duplicate of [String concatenation in MySQL](https://stackoverflow.com/questions/5975958/string-concatenation-in-mysql) – aynber Jul 29 '19 at 15:00
  • You do not say which DBMS you're using. The dupe I selected was for MySQL, but other databases use different syntax. – aynber Jul 29 '19 at 15:00

1 Answers1

1

+ is not the normal way to concatenate strings in SQL. The standard operator is || and the function concat() is usually available:

UPDATE users
    SET name = CONCAT(name, :name)
    WHERE id = :id;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks for the answer. It works. what if name value is integer and i want output to be like 1+1 ie 2 – Sashi Jul 29 '19 at 15:08
  • @Sashi . . . If you want addition, then use `+`. However, a numeric data type for a column called `name` would be unusual. – Gordon Linoff Jul 29 '19 at 15:18