如何在数据库MySQL中插入一个id数组?

时间:2018-05-07 12:43:45

标签: php mysql arrays pdo

我正在尝试将一个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) } 

1 个答案:

答案 0 :(得分:0)

如果您的占位符为:id:pm(如prepare()所示),则必须在:id中使用bindParam()

See php documentation