PHP PDO更新语句失败

时间:2013-06-20 19:49:18

标签: php session pdo sql-update

下面的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绑定参数,但我没有看到任何异常。传递正确的值,我仔细检查拼写错误。 我试图完成的是在插入数据库后根据firstname-lastname和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'];

2 个答案:

答案 0 :(得分:2)

尝试这样做:

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

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

答案 1 :(得分:2)

user_id = :userid

之后你有一个右括号

尝试以下方法:

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