簡體   English   中英

使用PHP和MySQL從購物車中刪除商品

[英]Deleting an item from my shopping cart using PHP and MySQL

我正在學習如何使用此StackOverflow,所以請多多包涵。 如果您需要進一步的服務,我可以提供。 如果您可以幫助我,我可以單擊您答案上的對勾。

我的代碼正在運行,但是有一個小故障。 當前,我有$ i = 0,我的代碼從表單中刪除了$ i。 問題是,例如,當刪除$ i = 2時,$ i = 3變成$ i = 2,並且我無法再從購物車中刪除該項目,因為現在它與刪除的同一$ i 。

這是我的代碼:

     if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    if (count($_SESSION["cart_array"]) <= 1) {
        unset($_SESSION["cart_array"]);
        header("location: cart.php");
        } else {
        unset($_SESSION["cart_array"]["$key_to_remove"]);
        //sort($_SESSION["cart_array"]);
    }
}

這是我的輸出循環:

$cartoutput = "";
$cartTotal="";
$totalwithtaxdisplay = 0;
$servicechargedisplay =0;
$grandtotaldisplay = 0;
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
    $cartoutput = "<div align='center'><font style='font-weight: bold; font-size: 20pt;'>Your order is currently empty.</font></div>";
}else{
    $i=0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        $result = mysqli_query($con,"SELECT * FROM menuitem WHERE id='$item_id' LIMIT 1");
            if (!$result) {
            printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
            exit();
            }
            //echo mysqli_num_rows($result);
        while ($row = mysqli_fetch_array($result)) {
                //item id is $each_item['item_id']; being pulled in from form on other page PID.
                $id = $row['id'];       
                $product_name = $row["name"];
                $price = $row["price"]; 
                $description = $row['description'];             
        }
            $tax = .065;
            $service = .18;
            $pricetotal = $price * $each_item['quantity'];
            $cartTotal = $pricetotal + $cartTotal;
            $totalwithtax = round($cartTotal + ($cartTotal * $tax), 2); //Order Items + Tax
            $totalwithtaxdisplay = number_format($totalwithtax, 2, '.', '');        //displays the decimals correctly
            $servicecharge = round($totalwithtax * $service, 2); //service charge
            $servicechargedisplay = number_format($servicecharge, 2, '.', '');      //displays the decimals correctly
            $grandtotal = round($totalwithtax + ($totalwithtax * $service), 2); //service charge
            $grandtotaldisplay = number_format($grandtotal, 2, '.', '');    //displays the decimals correctly           

            $cartoutput .= " <tr><td width='20%'> Order Item $i </td> 
                            <td width='40%'>  " . $product_name . "</td>
                            <td width='20%'> $" . $price . ".00</td>";

            $cartoutput .=" <td width='20%'><form action='cart.php' method='post'>
                            <input name='deleteBtn" . $item_id . "'type='submit' value='Remove This Item' />
                            <input name='index_to_remove' type='hidden' value='" . $i . "' />   
                            </form></td></tr>";
            $i++;

    }
}

稍后我回顯$ cartouput。 您可以在第二個$ cartouput上方的代碼中看到我正在使用的表單。 它擁有值$ i,但是刪除該值時,不允許我刪除已更新為新$ i的項。

不要將index_to_remove設置為$i ,而應將其設置為$item_id並在會話變量中。 這樣,您不再需要$i

另外,此行還留下了一個SQL注入漏洞: $item_id = $each_item['item_id']; 您至少應該使用mysqli_real_escape_string()對其進行mysqli_real_escape_string()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM