下面的 sql UPDATE 语句返回一个错误,但我不明白为什么:
Failed to run query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6
我已经对传递的数组进行了 vardump 以绑定参数,但我没有发现任何异常。传递了正确的值,我仔细检查了是否有错别字。我尝试完成的是在插入数据库后根据名字 - 姓氏和 user_id 自动生成用户名。
也许还有一个问题:您认为这有什么危害吗?如果有,您的建议是什么?我仍处于 PHP 学习阶段。
谢谢。
...
//Autogenerate user_name based on first name, last name and user_id (auto-increment)
$query_username = "
UPDATE user_tbl
SET
user_name = :username
WHERE
user_id = :userid
)
";
// The parameter values
$query_params_username = array(
':username' => $_SESSION['user']['first_name'].".".$_SESSION['user']['last_name'].$_SESSION['user']['user_id'],
':userid' => $_SESSION['user']['user_id']
);
try
{
// Execute the query against the database
$stmt_username = $db->prepare($query_username);
$stmt_username->execute($query_params_username);
}
catch(PDOException $ex)
{
//Not to be used in production
die("Failed to run query: " . $ex->getMessage());
}
$_SESSION['user']['username'] = $_SESSION['user']['first_name'].".".$_SESSION['user']['last_name'].$_SESSION['user']['user_id'];