PHP更改购物车数组中的数量

PHP更改购物车数组中的数量,php,post,get,Php,Post,Get,我知道如何更改购物车数组中的数量,但是我使用的是获取商品的详细信息,并且当刷新发生时,它看起来不再具有商品的id,而是返回这些错误 有没有办法在不刷新的情况下加载新数量 这是我的金额更改部分: <?php //section 3 if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_P

我知道如何更改购物车数组中的数量,但是我使用的是获取商品的详细信息,并且当刷新发生时,它看起来不再具有商品的id,而是返回这些错误

有没有办法在不刷新的情况下加载新数量

这是我的金额更改部分:

<?php
//section 3
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) {
        $i++;
        while (list($key, $value) = each($each_item)) {
            if ($key == "item_id" && $value == $item_to_adjust) {
                // That item is in cart already so let's adjust its quantity using array_splice()
                array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
            } // close if condition
        } // close while loop
    } // close foreach loop
}
?>

在get传入后添加可以减轻未定义var的错误,但是它会将项目信息更改为第一个条目,并且不会仅处理一个项目

<?php
//render cart
$cartOutput = "";
$cartTotal ="";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) <1){
    $cartOutput = "<h2 align=center'>Your shopping cart is empty</h2>";

}else{
    $i = 0;
    foreach($_SESSION["cart_array"] as $each_item){

        $item_id = $each_item['item_id'];

        include_once('config/database.php');
        include_once('object/chair.php');
        $database = new Database();
        $conn = $database->getConnection();
        $chair = new Chair($conn);
        $chair->id = $each_item['details_id'];
        $stmt = $chair->readDetails();    
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $product_name = $row['chair_name']; 
            $price = $row['PRICE'];
        }

        $pricetotal = $price  * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "en_US");
        $pricetotal = number_format($pricetotal, 2, '.', '\'');
        $cartOutput .="<tr>";
        $cartOutput .= "<td>".$product_name."</td>";
        $cartOutput .= "<td>$".$price."</td>";

        $cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="'.$each_item['quantity'].'" size="1" maxlength="2" />
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form></td>';

        // $cartOutput .= "<td>".$each_item['quantity']."</td>";
        $cartOutput .= "<td>$".$pricetotal."</td>";
        $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
        $cartOutput .="</tr>";
        $i++;