php
  • mysql
  • pdo
  • 2017-09-24 58 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

    相關問題