dou2347 2016-03-21 13:10 采纳率: 0%
浏览 30
已采纳

pdo没有正确绑定param [重复]

This question already has an answer here:

I've been stuck on this for about 3 days now and asked multiple people about this and no one seems to have an answer to me why this is not working. I cannot figure out why they aren't binding because the bindings work on the select statement but not the update. I know for a fact that $sessCheck['userid'] and $sessCheck['hwid'] are being set because I already printed them out to check if they were null or something.

The request inbound from slim

{"userid": "1000","hwid":"TESTING"}

The function

function updateHWID(){
        $request = Slim::getInstance()->request();
        //$bsreq = utf8_encode();
        $sessCheck = json_decode($request->getBody(), true, 9 );

        $db = getConnection();
        $sql = "SELECT userid,hwID FROM accounts WHERE userid = :userid";
        $stuff = $db->prepare($sql);
        $stuff->bindParam("userid", $sessCheck['userid']);
        $stuff->execute();
        $db = null;

        $rows = $stuff->fetch(PDO::FETCH_ASSOC);


        if ($rows['hwID'] != $sessCheck['hwid']) {
                $sql2 = "UPDATE accounts SET hwID=':hwid' WHERE userID = ':userid';";

                try {
                        $db2 = getConnection();
                        $stmt = $db2->prepare($sql2);
                        //these two param's are not binding
                        $stmt->bindParam("userid", $sessCheck['userid']);
                        $stmt->bindParam("hwid", $sessCheck['hwid']);         
                        $stmt->execute();
                        //$rt = $stmt->fetch(PDO::FETCH_ASSOC);
                        //$stmt->debugDumpParams();
                } catch(PDOException $e) {
                        echo '{"error":{"text":'. $e->getMessage() .'}}';
                }       
        }

}

This is the result incoming on the sql log

      1372 Query     UPDATE accounts SET hwID=':hwid' WHERE userID = ':userid'

I've also tried this as well as using the which also didn't work

$stmt->bindParam(":userid", $sessCheck['userid']);
$stmt->bindParam(":hwid", $sessCheck['hwid']);   

Then I tried this too and it didn't work

$stmt = $db2->prepare("UPDATE accounts SET hwID='?' WHERE userID = '?';");
$stmt->bindParam(1, $sessCheck['hwid'], PDO::PARAM_STR);        
$stmt->bindParam(2, $sessCheck['userid'], PDO::PARAM_INT);
</div>
  • 写回答

1条回答 默认 最新

  • dongyanghan0556 2016-03-21 13:12
    关注

    Take the binded parameter names out of their single quotes.

    so:
    $sql2 = "UPDATE accounts SET hwID=:hwid WHERE userID = :userid;";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义