簡體   English   中英

如何在php pdo中使用會話變量更新表?

[英]how to update table with session variable in php pdo?

我有一張名為 post_data 的表,因為我想根據會話變量更新列。

這是我的查詢。

$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....";
        }

這是 USER 類中的 runQuery() 實現

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

我有這樣的錯誤

致命錯誤:未捕獲的 PDOException:SQLSTATE[42000]:語法錯誤或訪問沖突:1064 您的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,以在 C:\\xampp\\htdocs\\DSLR_proj\\profile.php 中的第 8 行 'upd_date='2017-09-24 21:29:18' 附近使用正確的語法:97

你失蹤了,就在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM