gpt4 book ai didi

php mysql bind_param 仅插入单行

转载 作者:行者123 更新时间:2023-11-29 10:40:42 25 4
gpt4 key购买 nike

任何人都可以解释为什么下面的代码只在 mysql 数据库中插入单行。我使用 echo 函数检查数组,它返回两行。

$counter=1; // just assume
$orderid=date("mY").'-'.str_pad($counter, 4, '0', STR_PAD_LEFT);
$stmt = $stmt_connect->prepare("INSERT INTO tblorder_detail(orderid,idno,productid,side,quantity,price,design,days,uploadtime,deliverytime,filecheck,isdelete) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$i=0;
foreach ($saved_cart_items as $key => $value) {
$i++;
$isdelete=0;

$stmt->bind_param("siiiidiissii", $orderid, $i, $value['productid'], $value['side'], $value['quantity'], $value['price'], $value['design'], $value['days'], $value['uploadtime'], $value['deliveryid'], $value['filecheck'], $isdelete );
$stmt->execute();
}

最佳答案

第一:您需要在 foreach 之外声明 $i=1; 。否则第一次 $i 是 undefined 。所以它抛出错误。在下一个循环中它将变成 1 因此它只插入第二行。

第二:您可以像下面这样绑定(bind)变量

$stmt = $stmt_connect->prepare("INSERT INTO tblorder_detail(orderid,idno,productid,side,quantity,price,design,days,uploadtime,deliverytime,filecheck,isdelete) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("siiiidiissii", $orderid, $i, $productid, $side, $quantity, $price, $design, $days, $uploadtime, $deliveryid, $filecheck, $isdelete );
$i=1;
foreach ($saved_cart_items as $key => $value) {
$i++;
$isdelete=0;
$productid = $value['productid'];
$side=$value['side'];
$value['quantity'];
$price= $value['price'];
$design= $value['design'];
$days= $value['days'];
$uploadtime=$value['uploadtime'];
$deliveryid=$value['deliveryid'];
$filecheck=$value['filecheck'];


$stmt->execute();
}

关于php mysql bind_param 仅插入单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com