简体   繁体   English

如何在数据库MySQL中插入ID数组?

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

I'm trying to insert an array of IDs into a database table, but at the moment it only inserts 1 row when it's meant to do multiple rows. 我正在尝试将ID数组插入数据库表中,但目前,它只打算执行多行操作时,只会插入1行。 My array of IDs contains just (filmid) Does anyone know what the problem is? 我的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();
    }

}

I've tried to loop over the array with: 我试图用以下方法遍历数组:

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

This is what outputted: 这是输出的内容:

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) } 

If your placeholder is :id and :pm (as in prepare() ) then you must use :id in bindParam() 如果您的占位符是:id:pm (如prepare() ),则必须在bindParam()使用:id

See php documentation 见PHP文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM