Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

php - How to concatenate two string during MySQL UPDATE query?

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.

Created record using this MySql query

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

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.

Any errors in the UPDATE query?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Change your code to this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...