php - 如何将 id 数组插入数据库 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
and :pm
(如prepare()
),那么您必须使用:id
inbindParam()
推荐阅读
- ruby-on-rails - 为什么这些活动记录查询结果会出现在我的视图中?
- ubuntu - 调整大小后重新启动我的 ec2 实例后,如何确保所有正在运行的进程重新启动?
- python - 酸洗非常大的物体时如何处理酸洗错误
- mysql - MYSQL查询返回两天内价格涨幅最高的值
- python - Scikit-learn 导入约定
- sql - 在 Postgres 中尊重时区的同时按天计算事件
- javascript - 基于javascript中的键的聚合总和
- c++ - 使用可变参数函数泛化 is_detected
- javascript - 如何使用 scrollIntoView() 同时平滑滚动两个元素?
- java - IntelliJ 在 Gradle Kotlin-DSL 中错误地抱怨它找不到“java.lang.String”