Results 1 to 2 of 2

Thread: shopping cart error help please

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    shopping cart error help please

    hello,

    ive also mennt to post code for my shopping cart i have it working but i cant get the checkout to work it keeps saying it empty when i press the paypal button can you help please?.

    ill do the rating aswell.

    This is cart.php

    Code:
    <?php
        session_start();
        //session_destroy();
        require('scripts/connect.php');
        function  display_cart(){
            global $dbc;
            $q = mysql_query("SELECT `id`, `image`, `name`, `desc`, `price` FROM cart WHERE `quantity` > 0");
            $num = mysql_num_rows($q);
            while($fetch = mysql_fetch_assoc($q)){
                echo '<img src="pics/'.$fetch['image'].'" width="120" height="120" /><br /><span id="name">'.$fetch['name'].'</span><br/>'.$fetch['desc'].'<br/> $ '.$fetch['price'].' <a href="cart.php?add='.$fetch['id'].'">Add to Cart</a><br/><br/>';
                
            }
        }
        
        if(isset($_GET['add']) && !empty($_GET['add'])){
            $id = $_GET['add'];
            $q = mysql_query("SELECT `id`, `quantity` FROM cart WHERE `id`= '".$id."'");
            while($quantity = mysql_fetch_assoc($q)){
                header('Location:products.php');
                if($quantity['quantity'] != @$_SESSION['cart_'.$id]){
                    echo @$_SESSION['cart_'.$id]+=1;
                }
            }
        }
        
        if(isset($_GET['remove'])){
            $_SESSION['cart_'.$_GET['remove']]--;
            header('Location:products.php');
                
        }
        
        if(isset($_GET['delete'])){
            $_SESSION['cart_'.$_GET['delete']]=0;
            header('Location:products.php');
                
        }
        
        function paypal_item(){
          $num=0;
            foreach($_SESSION as $name => $value){
                if($value > 0){
                    if(substr($name,0,5) == 'cart_'){
                        $id = substr($name,5,(strlen($name-5)));
                        $q = mysql_query("SELECT `id`, `shipping`, `name`, `price` FROM cart WHERE `id` = '".$id ."'");
                            while($get_row = mysql_fetch_assoc($q)){  
                              $num++;
                              echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
                              echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
                              echo '<input type="hidden" name="amout_'.$num.'" value="'.$get_row['price'].'">'; 
                              echo '<input type="hidden" name="shipping_'.$num.'" value="'.$get_row['shipping'].'">';
                              echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';        
        }
            }
            }
            }
        }
        
        function product(){
            $net_payment=0;
            foreach($_SESSION as $name => $value){
                if($value > 0){
                    if(substr($name,0,5) == 'cart_'){
                        $id = substr($name,5,(strlen($name-5)));
                        $q = mysql_query("SELECT `id`, `shipping`, `name`, `price` FROM cart WHERE `id` = '".$id ."'");
                            while($get = mysql_fetch_assoc($q)){
                                $total = $value * $get['price'];
                                echo $get['name'].'X'.$value.'@'.$get['price'].'=$ '.$total.'<a href="cart.php?add='.$id.'"> [+] </a><a href="cart.php?remove='.$id.'"> [-] </a><a href="cart.php?delete='.$id.'"> [Delete] </a><br /><br />';
                        }
                    }
                        $net_payment+=$total;
                }
            }
            if($net_payment == 0){
                echo "Your Shopping Cart Is Empty!.";
            } else{
                echo "Total = £".$net_payment.'<br /><br />';
                ?>
                
                <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                    <input type="hidden" name="cmd" value="_cart">
                    <input type="hidden" name="business" value="mark@mbdues.co.uk">
                    <input type="hidden" name="upload" value="1">
                    <?php paypal_item(); ?>
                    <input type="hidden" name="currency_code" value="USD">
                    <input type="hidden" name="amount" value="<?php echo $net_payment; ?>">
                    <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
                </form>
                
                <?php
            }
        }
    ?>
    this is the products.php page

    Code:
    <?php include('styles/top.php'); ?>
    
        <div id="full">
        
        <header>
        
            <h1>Yan Sum Kune - Shop</h1>
        
        </header>
        
        <?php include("cart.php"); ?>
        
        <div id="main_div">
            <h3> Shopping Cart</h3>
            <div id="division">
                <section id="main_section">
                    <?php display_cart(); ?>
                </section>
            
                <aside id="side">
                    <span class="your_cart">Your Cart</span>&nbsp;&nbsp;<br /><br />
                    <?php product(); ?>
                </aside>
                
            </div>
        
        </div>
        
        </div>
    
    <?php include('styles/bottom.php'); ?>
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  2. #2
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: shopping cart error help please

    I don't have a direct solution, but I may have a few steps that could help identify the issue...

    Could you add a line to the function product() "print_r($_SESSION);" to see what is actually inside "$_SESSION".

    Also, you're using the variable $total in product before it is assigned a value, which means that it won't always have a value. Won't this result in an error if sessions other than "cart_" x are assigned? (see last line is outside the if statement, in which $total receives its value.)

    Code:
                    if(substr($name,0,5) == 'cart_'){
                        $id = substr($name,5,(strlen($name-5)));
                        $q = mysql_query("SELECT `id`, `shipping`, `name`, `price` FROM cart WHERE `id` = '".$id ."'");
                            while($get = mysql_fetch_assoc($q)){
                                $total = $value * $get['price'];
                                echo $get['name'].'X'.$value.'@'.$get['price'].'=$ '.$total.'<a href="cart.php?add='.$id.'"> [+] </a><a href="cart.php?remove='.$id.'"> [-] </a><a href="cart.php?delete='.$id.'"> [Delete] </a><br /><br />';
                        }
                    }
                        $net_payment+=$total;
    is this line actually getting executed?

    Code:
                                echo $get['name'].'X'.$value.'@'.$get['price'].'=$ '.$total.'<a href="cart.php?add='.$id.'"> [+] </a><a href="cart.php?remove='.$id.'"> [-] </a><a href="cart.php?delete='.$id.'"> [Delete] </a><br /><br />';

    and what value does $get['price'] have? if it has zero or no value, then you're multiplying by 0, which always results in 0.

    try and print the values out so you have a detailed overview of your variables.
    Last edited by Justa Lol; May 18th, 2015 at 03:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width