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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料