foreach 未执行 - 仅显示第一个变量输入
foreach not executing - only showing the first variable input
我试图弄清楚为什么我的 foreach 循环没有提供一个以上的变量(第一个变量)- 我正在尝试创建一个电子商务网站,这是我的购物车的保存功能。
两个 sql 语句都是正确的并且进入了我的数据库。
有两个 table - 一个发票 table 和另一个 table 我将显示订购的产品。
我正在使用重置功能来获取我在另一个功能中上传的图像的 ID。
大家有什么想法吗?
public static function submit()
{
$nOrderNum = date("Y-m-d").mt_rand();
$orderDate = date("Y-m-d h:m:s");
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$imgID = AllMerch::getImgs();
$orderToggle = 0;
$value = reset($imgID);
$con = Db::connect();
mysqli_query($con,"
INSERT INTO invoice
(
nOrderNum,
strOrderDate,
strName,
strLast,
strEmail,
nPhone,
strAddress,
nPrice,
bOrderToggle
) VALUES (
'".$nOrderNum."',
'".$orderDate."',
'".$firstname."',
'".$lastname."',
'".$email."',
'".$phone."',
'".$address."',
'".Cart::checkoutTotal()."',
'".$orderToggle."'
)");
$invoiceID = mysqli_insert_id($con);
foreach($_SESSION["arrCart"] as $merch){
mysqli_query($con,"
INSERT INTO merch_history
(
nInvoiceID,
nProductsID,
nQty,
nPrice,
nImgID
) VALUES (
'".$invoiceID."',
'".$merch["id"]."',
'".$merch["currentQty"]."',
'".$merch["price"]."',
'".$value."'
)");
$sql = "UPDATE products SET nQty = nQty - '".$merch["currentQty"]."' WHERE id = '".$merch["id"]."'";
$sql = "UPDATE images SET nInvoiceID = '".$invoiceID."' WHERE nSessionID = '".$_SESSION['id']."'";
mysqli_query($con,$sql);
$sql = "SELECT
merch_history.id,
merch_history.nInvoiceID,
merch_history.nProductsID,
merch_history.nQty,
merch_history.nPrice,
images.id AS imgID,
images.strImage AS strImg
FROM merch_history
LEFT JOIN images ON images.id = merch_history.nImgID
";
$_SESSION['arrCart'] = array();
return $invoiceID;
}
}
您的 return $invoiceID; 在 for 循环内,尝试在循环外删除它。
执行此操作,因为最后 2 个语句在循环中,如我所示将它们取出:
foreach($_SESSION["arrCart"] as $merch){
mysqli_query($con,"
INSERT INTO merch_history
(
nInvoiceID,
nProductsID,
nQty,
nPrice,
nImgID
) VALUES (
'".$invoiceID."',
'".$merch["id"]."',
'".$merch["currentQty"]."',
'".$merch["price"]."',
'".$value."'
)");
$sql = "UPDATE products SET nQty = nQty - '".$merch["currentQty"]."' WHERE id = '".$merch["id"]."'";
$sql = "UPDATE images SET nInvoiceID = '".$invoiceID."' WHERE nSessionID = '".$_SESSION['id']."'";
mysqli_query($con,$sql);
$sql = "SELECT
merch_history.id,
merch_history.nInvoiceID,
merch_history.nProductsID,
merch_history.nQty,
merch_history.nPrice,
images.id AS imgID,
images.strImage AS strImg
FROM merch_history
LEFT JOIN images ON images.id = merch_history.nImgID
";
}
$_SESSION['arrCart'] = array();
return $invoiceID;
我试图弄清楚为什么我的 foreach 循环没有提供一个以上的变量(第一个变量)- 我正在尝试创建一个电子商务网站,这是我的购物车的保存功能。 两个 sql 语句都是正确的并且进入了我的数据库。
有两个 table - 一个发票 table 和另一个 table 我将显示订购的产品。 我正在使用重置功能来获取我在另一个功能中上传的图像的 ID。
大家有什么想法吗?
public static function submit()
{
$nOrderNum = date("Y-m-d").mt_rand();
$orderDate = date("Y-m-d h:m:s");
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$imgID = AllMerch::getImgs();
$orderToggle = 0;
$value = reset($imgID);
$con = Db::connect();
mysqli_query($con,"
INSERT INTO invoice
(
nOrderNum,
strOrderDate,
strName,
strLast,
strEmail,
nPhone,
strAddress,
nPrice,
bOrderToggle
) VALUES (
'".$nOrderNum."',
'".$orderDate."',
'".$firstname."',
'".$lastname."',
'".$email."',
'".$phone."',
'".$address."',
'".Cart::checkoutTotal()."',
'".$orderToggle."'
)");
$invoiceID = mysqli_insert_id($con);
foreach($_SESSION["arrCart"] as $merch){
mysqli_query($con,"
INSERT INTO merch_history
(
nInvoiceID,
nProductsID,
nQty,
nPrice,
nImgID
) VALUES (
'".$invoiceID."',
'".$merch["id"]."',
'".$merch["currentQty"]."',
'".$merch["price"]."',
'".$value."'
)");
$sql = "UPDATE products SET nQty = nQty - '".$merch["currentQty"]."' WHERE id = '".$merch["id"]."'";
$sql = "UPDATE images SET nInvoiceID = '".$invoiceID."' WHERE nSessionID = '".$_SESSION['id']."'";
mysqli_query($con,$sql);
$sql = "SELECT
merch_history.id,
merch_history.nInvoiceID,
merch_history.nProductsID,
merch_history.nQty,
merch_history.nPrice,
images.id AS imgID,
images.strImage AS strImg
FROM merch_history
LEFT JOIN images ON images.id = merch_history.nImgID
";
$_SESSION['arrCart'] = array();
return $invoiceID;
}
}
您的 return $invoiceID; 在 for 循环内,尝试在循环外删除它。
执行此操作,因为最后 2 个语句在循环中,如我所示将它们取出:
foreach($_SESSION["arrCart"] as $merch){
mysqli_query($con,"
INSERT INTO merch_history
(
nInvoiceID,
nProductsID,
nQty,
nPrice,
nImgID
) VALUES (
'".$invoiceID."',
'".$merch["id"]."',
'".$merch["currentQty"]."',
'".$merch["price"]."',
'".$value."'
)");
$sql = "UPDATE products SET nQty = nQty - '".$merch["currentQty"]."' WHERE id = '".$merch["id"]."'";
$sql = "UPDATE images SET nInvoiceID = '".$invoiceID."' WHERE nSessionID = '".$_SESSION['id']."'";
mysqli_query($con,$sql);
$sql = "SELECT
merch_history.id,
merch_history.nInvoiceID,
merch_history.nProductsID,
merch_history.nQty,
merch_history.nPrice,
images.id AS imgID,
images.strImage AS strImg
FROM merch_history
LEFT JOIN images ON images.id = merch_history.nImgID
";
}
$_SESSION['arrCart'] = array();
return $invoiceID;