drlu11748 2013-06-20 19:49
浏览 359
已采纳

PHP PDO更新语句失败

The below sql UPDATE statement returns an error but I'm unable to see why:

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

I already did a vardump of the array that I pass to bind the parameters but I see nothing unusual. The correct values are passed and I double checked for typos. What I try to accomplish is to auto-generate a username based on firstname - lastname and user_id after insertion into the database.
Perhaps additional question: do you see any harm in that and if so, what is your suggestion? I'm still in PHP learning phase.

Thanks.

        ...

        //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条回答 默认 最新

  • douji6461 2013-06-20 20:01
    关注

    You had a closing parentheses after user_id = :userid

    Try the following:

    $query_username = "
        UPDATE user_tbl 
            SET
                user_name = :username 
            WHERE
                user_id = :userid
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化