i have a shopping cart and everything works but the update button, can you see any errors in my code?

function updateCart()
{
    $cartId     = $_POST['hidCartId'];
    $productId  = $_POST['hidProductId'];
    $itemQty    = $_POST['txtQty'];
    $numItem    = count($itemQty);
    $numDeleted = 0;
    $notice     = '';

    for ($i = 0; $i < $numItem; $i++) {
        $newQty = (int)$itemQty[$i];
        if ($newQty < 1) {
            // remove this item from shopping cart
            deleteFromCart($cartId[$i]);    
            $numDeleted += 1;
        } else {
            // check current stock
            $sql = "SELECT pd_name, pd_qty
                    FROM tbl_product 
                    WHERE pd_id = {$productId[$i]}";
            $result = dbQuery($sql);
            $row    = dbFetchAssoc($result);

            if ($newQty > $row['pd_qty']) {
                // we only have this much in stock
                $newQty = $row['pd_qty'];

                // if the customer put more than
                // we have in stock, give a notice
                if ($row['pd_qty'] > 0) {
                    setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the &quot;Quantity&quot; box. ');
                } else {
                    // the product is no longer in stock
                    setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');

                    // remove this item from shopping cart
                    deleteFromCart($cartId[$i]);    
                    $numDeleted += 1;                   
                }
            } 

            // update product quantity
            $sql = "UPDATE tbl_cart
                    SET ct_qty = $newQty
                    WHERE ct_id = {$cartId[$i]}";

            dbQuery($sql);
        }
    }

    if ($numDeleted == $numItem) {
        // if all item deleted return to the last page that
        // the customer visited before going to shopping cart
        header("Location: $returnUrl" . $_SESSION['shop_return_url']);
    } else {
        header('Location: cart.php');   
    }

    exit;
}

function isCartEmpty()
{
    $isEmpty = false;

    $sid = session_id();
    $sql = "SELECT ct_id
            FROM tbl_cart ct
            WHERE ct_session_id = '$sid'";

    $result = dbQuery($sql);

    if (dbNumRows($result) == 0) {
        $isEmpty = true;
    }   

    return $isEmpty;
}

/*
    Delete all cart entries older than one day
*/
function deleteAbandonedCart()
{
    $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
    $sql = "DELETE FROM tbl_cart
            WHERE ct_date < '$yesterday'";
    dbQuery($sql);      
}

?>

it is also linked to the config.phpwhich hasthe following code about update

if (!get_magic_quotes_gpc()) {
    if (isset($_POST)) {
        foreach ($_POST as $key => $value) {
            $_POST[$key] =  trim(addslashes($value));
        }
    }

    if (isset($_GET)) {
        foreach ($_GET as $key => $value) {
            $_GET[$key] = trim(addslashes($value));
        }
    }   
}

can someone help please

thanks

Recommended Answers

All 3 Replies

// update product quantity
$sql = "UPDATE tbl_cart
SET ct_qty = '$newQty'
WHERE ct_id ='$cartId[$i]'";

try that))

Member Avatar for diafol

If that doesn't work, place your code within code tags (so that is legible) and repost. Can't read it like that, sorry.

Hi adamleefe,

My name is ANthony and I am having exactly the same problem as what u had? Did anyone help u to solve the "Update Cart" buttom. If so, pls could you help me. My email address is [snipped]. Tks for your help in advance.

Cheers,

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.