Hello,

I am trying to update or delete item from cart but it;s not oking

    session_start();
    include("includes/layout/header.php");
    include_once("includes/connection.php");
    function get_product_name($pid){

        global $connection;
        $result=mysqli_query($connection, "select product_name from products where prod_id=$pid");
        $row=mysqli_fetch_array($result);
        return $row['product_name'];
    }

    function get_price($pid){
    global $connection;
        $result=mysqli_query($connection, "select product_price from products where prod_id=$pid");
        $row=mysqli_fetch_array($result);
        return $row['product_price'];
    }
    function get_order_total(){
        $max=count($_SESSION['cart']);
        $sum=0;
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_price($pid);
            $sum+=$price*$q;
        }
        return $sum;
    }
?>
<script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>    

Code for cart

<form name="form1" method="post">
            <div id="cart">
            <div class="cart-items">
                                                <table class="styled-table">
                                                    <thead>
                                                        <tr>
                                                            <th class="col_product text-left">Product</th>
                                                            <th class="col_remove text-right">&nbsp;</th>
                                                            <th class="col_qty text-right">Qty</th>
                                                            <th class="col_single text-right">Single</th>                                                         
                                                            <th class="col_total text-right">Total</th>
                                                        </tr>
                                                    </thead>
                                                    <?php
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
            ?>
                                                    <tbody>                                  

                                                        <tr>
                                                            <td class="col_product text-left">
                                                                <div class="image visible-desktop">
                                                                    <span class="single-price"><?=get_product_name($pid)?></span>
                                                                </div>
                                                            </td>

                                                            <td class="col_remove text-right">
                                                                <a href="javascript:del(<?=$pid?>)">Remove</a>
                                                                    <i class="icon-trash icon-large"></i>
                                                                </a>
                                                            </td>

                                                            <td class="col_qty text-right">
                                                                <input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" >
                                                            </td>

                                                            <td class="col_single text-right">
                                                                <span class="single-price">$<?=get_price($pid)?></span>
                                                            </td>
<?php

}
?>
                                                            <td class="col_total text-right">
                                                                <span class="total-price">$<?=get_order_total()?></span>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
            </div>

            <div class="box-footer">
                            <div class="pull-left">
                                <input type="button" class="btn btn-small" value="Continue Shopping" onclick="window.location='index.php'" />        
                            </div>

                            <div class="pull-right">
                                   <input type="button" value="Update Cart" onclick="update_cart()" class="btn btn-small mm20">

                                    <input type="button" value="Place Order" class="btn btn-primary btn-small mm20" onclick="window.location='billing.php'">

                                    </form>
                            </div>
                        </div>

Dani AI

Generated

— the immediate problem is that the page’s JavaScript sets document.form1.pid and document.form1.command before submitting, but the form shown does not include hidden inputs named pid or command. Without those fields the server never receives the requested action. Also, naming each quantity input like product123 makes server-side parsing brittle; using an array (for example qty[123]) is simpler and less error-prone.

Fix summary (safe, minimal changes)

  • Add two hidden fields inside the <form name="form1" method="post">: command and pid.
  • Change per-item qty inputs to an array name such as qty[123] (example below).
  • On submit, the server-side handler should read $_POST['command'], cast incoming IDs to integers, update/remove items in $_SESSION['cart'], then redirect to avoid duplicate submissions.

Example HTML (illustrative only — replace 123 and 2 with the dynamic values in the template):

<input type="hidden" name="command" value="">
<input type="hidden" name="pid" value="">

<!-- per-item qty -->
<input type="text" name="qty[123]" value="2" maxlength="3" size="2">

Example server-side handler (illustrative):

session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cmd = $_POST['command'] ?? '';
    if ($cmd === 'clear') {
        unset($_SESSION['cart']);
    } elseif ($cmd === 'delete' && !empty($_POST['pid'])) {
        $pid = (int) $_POST['pid'];
        foreach ($_SESSION['cart'] as $k => $it) {
            if ((int)$it['productid'] === $pid) { unset($_SESSION['cart'][$k]); break; }
        }
        $_SESSION['cart'] = array_values($_SESSION['cart']);
    } elseif ($cmd === 'update' && !empty($_POST['qty']) && is_array($_POST['qty'])) {
        foreach ($_POST['qty'] as $pid => $q) {
            $pid = (int)$pid; $q = max(0, (int)$q);
            foreach ($_SESSION['cart'] as $k => $it) {
                if ((int)$it['productid'] === $pid) {
                    if ($q === 0) unset($_SESSION['cart'][$k]); else $_SESSION['cart'][$k]['qty'] = $q;
                    break;
                }
            }
        }
        $_SESSION['cart'] = array_values($_SESSION['cart']);
    }
    header('Location: cart.php'); exit;
}

Extra notes and quick checks (responds to ): common symptoms are “clicking Remove does nothing” or “Update appears to submit but quantities don’t change.” Verify with browser DevTools → Network that the POST body contains command, pid (for delete) or qty[...] (for update). Use var_dump($_POST) / var_dump($_SESSION['cart']) briefly to inspect server-side values. Always cast IDs to (int), validate quantities, and switch to prepared statements for DB lookups to avoid injection.

Recommended Answers

All 3 Replies

Can anyone help me out with this concirn if available

Can anyone help me out with this concirn

What exactly is not working? Are you getting an error? What are you trying to achieve?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.