0

下面的 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'];
4

2 回答 2

2

尝试这样做:

$query_username = "
                     UPDATE  `user_tbl` 
                     SET     `user_name` = :username 
                     WHERE   `user_id` = :userid
                  ";

您的代码中似乎有一个丢失的)字符。

于 2013-06-20T19:52:39.697 回答
2

你有一个右括号之后user_id = :userid

尝试以下操作:

$query_username = "
    UPDATE user_tbl 
        SET
            user_name = :username 
        WHERE
            user_id = :userid
";
于 2013-06-20T20:01:43.080 回答