任何人都可以解释为什么下面的代码只在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();
}
答案 0 :(得分:1)
第一名:您需要在$i=1;
之外声明foreach
。否则在第一次$i
未定义。所以这是投掷错误。在下一个循环中,它将变为1
,因此它仅插入第二行。
第二名:您可以绑定变量,如下所示
$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();
}