繁体   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