I'm nearly finish with a site template for a shopping cart.

I am encountering a problem, updating of quantity. Say:

On the Product Table page: If the user clicks on the same product to add it, only the quantity should increment on the Cart page (Quantity is displayed using a textbox).

On the Cart page: The user can increase or decrease the quantity by manually typing the number on the textbox I mentioned above, by clicking on the update button below the cart table.

That is where my problem is, even If I enter another number (say 2), the price of the product should be multiplied to that number (say 800 x 2), instead, the original quantity (1) remains and so is the price (800 instead of 1600).

Where Am I getting the error.
Attached are the files for selecting product, adding to cart up to placing the order.

Thanks in advance.

Recommended Answers

All 2 Replies

Member Avatar for diafol

You may get more interest if you post your code. Not many of us are inclined to download files from an untrusted source, i.e. you.

Best if you strip the code down to its relevant sections - don't include any css/styling.

Ok. So 1st, is to add the product to cart.

<?php
    include("includes/db.php");
    include("includes/functions.php");



    if(ISSET($_POST['command']) && ISSET($_POST['productid'])){
        $_POST['command']=='add';
        $_POST['productid']>0;
        $pid=$_POST['productid'];
        $user = $_SESSION['SES_UNAME'];

        addtocart($pid,1);
        header("location: shoppingcart.php");
        exit();
    }

?>

//--------JAVASCRIPT TO ADD THE ITEM--------------

<script language="javascript">
    function addtocart(pid){
        document.form1.productid.value=pid;
        document.form1.command.value='add';
        document.form1.submit();
    }
</script>

//--------PHP TO DISPLAY PRODUCT INFO AND TO ACTIVATE THE JAVASCRIPT--------------

<?php
            $result=mysqli_query($con,"SELECT * FROM products") or die('Connection Error.<br/><br/>'  .mysql_error());
            while($row=mysqli_fetch_array($result)){
        ?>
        <tr>
            <td><img src="admin/<?php echo $row['image']?>" height='100' width='100' /></td>
            <td><br>  <b><?php echo $row['pname']?></b><br />
                    <?php echo $row['pdesc']?><br />
                    Price:<big style="color:green">
                        <?php echo $row['price']?></big>
                    <br><br><input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['pId']?>)" /><br><br>

            </td>

Now the processed information will be stored to variables (including sessions) using a function.

<?php


    function get_product_name($pid){
    $result=mysqli_query($con,"SELECT pname FROM products where pId=$pid") or die("SELECT pname FROM products where pId=$pid"."<br/><br/>".mysql_error());
        $row=mysqli_fetch_array($result);
        return $row['pname'];
    }
    function get_price($pid){
        $con = mysqli_connect('localhost', 'abra', 'abra','abra') or die("Could not connect database");
        $result=mysqli_query($con,"SELECT price FROM products where pId=$pid") or die("SELECT pname FROM products where pId=$pid"."<br/><br/>".mysql_error());
        $row=mysqli_fetch_array($result);
        return $row['price'];
    }
    function remove_product($pid){
        $pid=intval($pid);
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            if($pid==$_SESSION['cart'][$i]['productid']){
                unset($_SESSION['cart'][$i]);
                break;
            }
        }
        $_SESSION['cart']=array_values($_SESSION['cart']);
    }
    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;
    }
    function addtocart($pid,$q){
        if($pid<1 or $q<1) return;

        if(is_array($_SESSION['cart'])){
            if(product_exists($pid)) return;
            $max=count($_SESSION['cart']);
            $_SESSION['cart'][$max]['productid']=$pid;
            $_SESSION['cart'][$max]['qty']=$q;
        }
        else{
            $_SESSION['cart']=array();
            $_SESSION['cart'][0]['productid']=$pid;
            $_SESSION['cart'][0]['qty']=$q;
        }
    }
    function product_exists($pid){
        $pid=intval($pid);
        $max=count($_SESSION['cart']);
        $flag=0;
        for($i=0;$i<$max;$i++){
            if($pid==$_SESSION['cart'][$i]['productid']){
                $flag=1;
                break;
            }
        }
        return $flag;
    }

?>

Then display the cart.

<?php

    include("includes/db.php");
    include("includes/functions.php");

    $currentuser = &$_GET['user'];

    if(ISSET($_REQUEST['command']) && ISSET($_REQUEST['pid'])){
        $_REQUEST['command']=='delete';
        $_REQUEST['pid']>0;
        remove_product($_REQUEST['pid']);
    }
    else if(ISSET($_REQUEST['command'])){
        $_REQUEST['command']=='clear';
        unset($_SESSION['cart']);
    }
    else if(ISSET($_REQUEST['command'])){
        $_REQUEST['command']=='update'; 
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=10){
                $_SESSION['cart'][$i]['qty']=$q;
            }
            else{
                $msg='Some products were not updated!';
            }
        }
    }

?>

//-------JAVASCRIPT TO PERFORM COMMANDS---------

<script language="javascript">
    function del(pid){
        if(confirm('Do you really want 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>

//-----------DISPLAY ORDER DETAILS AND OPTIONS--------

<?php
            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Quantity</td><td>Amount</td><td>Options</td></tr>';
                $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;
            ?>
                    <tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
                    <td> <?php echo get_price($pid)?></td>
                    <td><input type="text" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /></td>                    
                    <td> <?php echo get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?php echo $pid?>)">Remove</a></td></tr>
            <?php                    
                }
            ?>
                <tr><td colspan="2"><b>Order Total: <?php echo get_order_total()?></b></td>
                    <td colspan="5" align="right">
                        <input type="button" value="Update Cart" onclick="update_cart()">

                        <input type="hidden" name="cust_id" value="<?php $currentuser; ?>">
                        <input type="button" value="Place Order" onclick="window.location='billing.php'">
                    </td>
                </tr>

            <?php
            }
            else{
                echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
            }
        ?>

The adding and removing works, just that the update on cart page isn't working. The quantity is placed in a textbox which is editable, so that if the user change the quantity, say from 1 to 2, then the price will be multiplied by 2. But that is the problem, it won't.

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.