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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝