如何使用 mysql 在 INSERT 期间连接和保存名字和姓氏?

How to concatenate and save first name and last name during INSERT using mysql?

我正在尝试连接语言和代码作为第二个查询并保存到数据库字段 litcode。 这是我的代码

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO library (spot,code,language,count) values(?, ?, ?,?);UPDATE library SET litcode = CONCAT(language, code);";
$q = $pdo->prepare($sql);
$q->execute(array($spot,$code,$language,$count));
Database::disconnect();

收到此错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /home/booking/public_html/x/concat.php:46 Stack trace: #0 /home/booking/public_html/x/concat.php(46): PDOStatement->execute(Array) #1 {main} thrown in /home/booking/public_html/x/concat.php on line 5

这是代码中的第 5 行 $q->execute(array($spot,$code,$language,$count)); 如何添加连接词 litcode?

你可以简化你的代码,当你只需要 INSERT 正确的信息时,你不需要 UPDATE 所有的记录,看这个代码:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO library (spot,code,language,count,litcode) values(?,?,?,?,CONCAT(language,code))";
$q = $pdo->prepare($sql);
$q->execute(array($spot,$code,$language,$count));
Database::disconnect();