簡體   English   中英

如何在數據庫MySQL中插入ID數組?

[英]How do I insert an array of id into database MySQL?

我正在嘗試將ID數組插入數據庫表中,但目前,它只打算執行多行操作時,只會插入1行。 我的ID數組僅包含(filmid)有人知道問題出在哪里嗎?

$pm = "2";
$id = "2";             

if($stmt1->execute())
{
    $films=array();
    foreach($_SESSION['products'] as $key=>$product)
    {
        array_push($films, $product['filmid']);
        $f_id  = implode(",", $films);
        $stmt2 = $this->conn->prepare("INSERT INTO `filmPurchase` (`fpid`, `payid`, `filmid`, `shopid`, `custid`, `price`) VALUES (NULL, :id, :f_id, :pm, :pm, '8')");
        $stmt2->bindParam('pm',$pm);
        $stmt2->bindParam('id',$id);
        $stmt2->bindParam('f_id',$f_id);
        $stmt2->execute();
    }

}

我試圖用以下方法遍歷數組:

foreach($_SESSION['products'] as $key=>$product)
{
    var_dump($key);
    var_dump($product);
}

這是輸出的內容:

int(29) array(4) { ["qty"]=> int(1) ["filmtitle"]=> string(45) "The Lord of 
the Rings: The Return of the King" ["filmid"]=> string(2) "29" ["price"]=> 
float(6.99) } 

如果您的占位符是:id:pm (如prepare() ),則必須在bindParam()使用:id

見PHP文檔

暫無
暫無

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

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