php
  • mysql
  • pdo
  • 2017-09-24 124 views -1 likes 
    -1

    我有一个名为post_data的表,因为我想根据会话变量更新列。如何使用php pdo中的会话变量更新表?

    这是我的查询。

    $id = $_SESSION['userSession']; 
    
    $stmt = $user_home->runQuery("UPDATE post_data 
                 set 
                 cam_name='$cname', 
                 cam_model ='$model', 
                 cam_rent='$rent', 
                 cam_img='$upic', 
                 mobile='$umob' 
                 upd_date='$jdate' 
                 where userID='$id' 
                 "); 
    
         $stmt->bindParam(':cname',$camname); 
         $stmt->bindParam(':model',$modelname); 
         $stmt->bindParam(':rent',$rentpday); 
         $stmt->bindParam(':upic',$userpic); 
         $stmt->bindParam(':umob',$usermob); 
         $stmt->bindParam(":jdate",$upd_date); 
    
         if($stmt->execute()) 
         { 
    
          $successMSG = "Record saved success"; 
         } 
         else 
         { 
          $errMSG = "error while inserting...."; 
         } 
    

    这是runQuery()用户类实现

    public function runQuery($sql) 
    { 
        $stmt = $this->conn->prepare($sql); 
        return $stmt; 
    } 
    

    我有错误这样

    致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064你的SQL语法有错误;检查对应于您的MariaDB服务器版本的手册,以找到在'upd_date ='附近使用的正确语法2017-09-24 21:29:18'where'在C:\ xampp \ htdocs \ DSLR_proj \ profile.php中的第8行:97

    回答

    0

    您正在输入mobile='$umob'之后缺少,

    另外,$cname不一样:cname。我宁愿使用占位符作为?而不是任何特定的字符串以避免任何错字。

    另外,您缺少对userID列的绑定。

    UPDATE post_data 
         set cam_name=?,cam_model =?, cam_rent=?, cam_img=?, mobile=?, upd_date=? 
         where userID=? 
    
        $stmt->bindParam(1,$camname); 
        $stmt->bindParam(2,$modelname); 
        $stmt->bindParam(3,$rentpday); 
        $stmt->bindParam(4,$userpic); 
        $stmt->bindParam(5,$usermob); 
        $stmt->bindParam(6,$upd_date); 
        $stmt->bindParam(7,$id); // you are missing this as well 
    
    +0

    感谢它的工作原理...非常感谢 –

    +0

    雅非常感谢@Ravi其现在的工作 –

    +0

    @lohithkumar你需要接受的答案 – Ravi

    相关问题