How to concatenate two rows during MySQL UPDATE query?

Use case: Created record Spot, Code, Language, Count ... later it turned out that the entered language was wrong. Tried changing the language using an UPDATE query. The concatenated string still displays the same. The updated language string was not changed in the concatenated string.

Created a record using this MySql query

$sql = "INSERT INTO library (spot,code,language,count,litcode) values(?,?,?,?,CONCAT(language,'-',code))";

      

Tried UPDATE language using this MySql query

Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$sql = "UPDATE literatures set spot = ?, code = ?,language = ?, count =?, litcode = CONCAT(language,'-',code) WHERE id = ?";
$q = $pdo->prepare($sql); 
$q->execute(array($spot,$code,$language,$count,$litcode,$id)); 
Database::disconnect();

      

Finally, the language was updated, but not updated in the concatenated string.

Any errors in the UPDATE query?

+3


source to share


1 answer


You are trying to tie $litcode

, but you have CONCAT(language,'-',code)

. You have 5 ?

in your request, but you are trying to bind 6 parameters.

Change the code as follows:



Database::connect(); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$sql = "UPDATE literatures set spot = ?, code = ?, language = ?, count = ?, litcode = ? WHERE id = ?";
$q = $pdo->prepare($sql); 
$q->execute(array($spot,$code,$language,$count,$litcode,$id)); 
Database::disconnect();

      

+3


source







All Articles