本文介绍了MySQL UPDATE查询期间如何串联两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:创建一条记录现货,代码,语言,数量...后来发现输入的语言是错误的.试图使用UPDATE查询来更改语言.串联字符串仍然显示相同.已更新的语言字符串在串联字符串中未更改.

Use case: Created a record Spot, Code, Language, Count...later found out language entered was wrong. Tried to change the language using UPDATE query. Concatenated string still show same. Updated language string not changed in the concatenated string.

使用此MySql查询创建的记录

Created record using this MySql query

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

使用此MySql查询尝试使用UPDATE语言

Tried to 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 language was updated but its not updating in the concatenated string.

UPDATE查询中有任何错误吗?

Any errors in the UPDATE query?

推荐答案

您正在尝试绑定$litcode,但是您拥有CONCAT(language,'-',code).您的查询中有5个?,但是您试图绑定6个参数.

You are trying to bind $litcode but you have CONCAT(language,'-',code). You have 5 ? in your query but you are trying to bind 6 params.

将代码更改为此:

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();

这篇关于MySQL UPDATE查询期间如何串联两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 17:03