General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 10th, 2003, 11:57 PM
bmy78 bmy78 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 bmy78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question shopping cart problem (newbie)

Hi all,

I've been having a problem building a basic shopping cart, which is modelled after the "persistent shopping cart" tutorial. It works fine, however, every time I hit the "refresh" button on my Web browser, it adds an additional item to the cart (but only when the quantity is set to 1, if I set it higher it won't add it). The code is below. Can anyone tell me what I'm doing wrong, I'm not sure what I'm seeing.

The cart.php code

PHP Code:
[SIZE=1]
<?
php
session_start
();

// set cookie
setcookie("cart_id"session_id(), time() + ((3600 24) * 30));

?>

<html>
<head>
<title>Buy My Junk.com</title>
<script language="JavaScript">
        function UpdateQty(item)
        {
            item_id = item.name;
            newQty = item.options[item.selectedIndex].text;
            document.location.href='cart.php?action=update_ite  m&id='+item_id+'&qty='+newQty;
        }
</script>    
</head>

<body>
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
    <!-- HEADER FOR LOGO, 468x60 BANNERS ----------------->
    <td colspan="2">
    <h1>Buy My Junk.com</h1>
    </td>
    </tr>
    <tr>
    <td width="85%" align="right">
    LINK | LINK | LINK | LINK | LINK
    &nbsp;
    </td>
    <td align="right">
    <input type="text" size="10" value="Search">&nbsp;<input type="button" value="Go!"></td>
    </tr>
<tr>
    <td width="15%" colspan="3" width="70%" valign="top">
    <h2>View Cart</h2>
    
    
        
            <?php
            
                
// includes
                
require("./data/functions.php");
                
                
// connect to db
                
db_connect();
                
                switch(
$_GET["action"])
                {
                    case 
"add_item":
                    {
                        
AddItem($_GET["id"], $_GET["qty"]);
                        
ShowCart();
                        break;
                    }
                    case 
"update_item":
                    {
                        
UpdateItem($_GET["id"], $_GET["qty"]);
                        
ShowCart();
                        break;
                    }
                    case 
"remove_item":
                    {
                        
RemoveItem($_GET["id"]);
                        
ShowCart();
                        break;
                    }
                    default:
                    {
                        
ShowCart();
                    }
                }
                
                
                
                
            
?>
            
        
    
</td>
</tr>    
<td colspan="3">&nbsp;</td>
</tr>
</table>




</td></tr>
</table>




</body>
</html>[/SIZE] 


and here's the ShowCart() function in the include file functions.php








PHP Code:
function ShowCart()
{
    
$result mysql_query("select * from cart inner join products on cart.item_id = products.item_id where cart.cookie_id = '" GetCartId() . "' order by products.item_name asc")
        or 
mysql_error($result);
    
    
// once we've retrieved the list, each item is displayed as part of a table, as a table row
    
echo "<table border='1' cellspacing='0' cellpadding='2' class='cart'>";
    
    
$color1 "gray";
    
$color2 "lightgray";
    
$row_count 0;    
    while(
$row mysql_fetch_array($result))
    {
        
// increment the total cost of all items
        
$totalCost += ($row["qty"] * $row["item_price"]);
    
        
// alternating table colors
        
$row_color = ($row_count 2) ? $color1$color2;
        
?>
         <tr><td bgcolor="<?php echo $row_color;?>">
         <font face='verdana' size='1' color='black'>
         <select name='<?php echo $row['item_id'];?>' onChange='UpdateQty(this)'
                 
        <?php
        
for ($i 1$i <= 20$i++)
        {
            echo 
"<option ";
            if (
$row["qty"] == $i)
            {
                echo 
" SELECTED ";
            }
            
            echo 
">" $i "</option>";
        }
        
?>
        </select></font></td>
        <td width="60%" height="25" bgcolor="<?php echo $row_color;?>">
        <font style="color:#FFFFFF;font-size:12pt;font-weight:bold;" face="verdana,arial,helvetica,sans-serif" size='1' color='black'>
         <?php echo $row["item_name"];?> </font></td>
        <td width="20%" height="25" bgcolor="<?php echo $row_color;?>">
        <font style="font-size:12pt;font-weight:bold;" face='verdana' size='1' color='white'>$  
        <?php echo number_format($row['item_price'], 2'.'',');?></font>        </td>
<td width="10%" height="25" bgcolor="<?php echo $row_color;?>">
        <font face='verdana' size='1' color='black'>
        <a href=cart.php?action=remove_item&id=<?php echo $row['item_id'];?>>Remove</a>
        </font></td>        </tr>
    <?php
    
    
// increase row count by 1
    
$row_count++;
    
    }
    
?>
    
    <tr><td width='100%' colspan='4'>
    <hr size='1' color='red' NOSHADE>
    </td></tr>
    <tr><td width='70%' colspan='2'>
    <font face='verdana' size='1' color='black'>
    <a href='products.php'><< Keep Shopping</a>
    </font></td>    
    <td width='30%' colspan='2'>
    <font face='verdana' size='2' color='black'>
    <b>Total: <?php echo number_format($totalCost2"."",");?> </b>
    </font></td></tr>
    <tr><td align="right" colspan="4">
    <a href="checkout.php">Checkout >></a>
    </td></tr>
    </table>
<?php    


Reply With Quote
  #2  
Old August 16th, 2003, 02:01 PM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Whenever you click on a link, the browser sends a request to the server.
If you hit refresh, the browser sends a new request to the server.
So the shopping cart gets 2 commands to add the product.
That's why new stuff is added.
I didn't read all of your code, so just say if I missed something.

Reply With Quote
  #3  
Old August 18th, 2003, 01:22 AM
jpipes jpipes is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jpipes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The problem is most likely in the AddItem method. Please post the code for that method so we can see what the issue is.

Reply With Quote
  #4  
Old August 18th, 2003, 10:26 PM
bmy78 bmy78 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 bmy78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Okay, here's the AddItem() function, along with UpdateItem():

PHP Code:
[small]
// The AddItem function is used to add items to your shopping cart. 
// It also checks to see if this item already exists in your shopping cart.

function AddItem($item_id$qty)
{
    
    
// MySQL query    
    
$result mysql_query("SELECT count(*) from cart WHERE cookie_id='" GetCartId() . "' and item_id = $item_id");
    
    
// set numRows variable to first row in array
    
$numRows $row[0];
    
    
// if numRows value equals 0
    // we will add the item into the cart table
    
    
if ($numRows == 0)
    {
        
// This item doesn't exist in the users cart.
        // We will add it with an insert query
        
        
mysql_query("Insert into cart(cookie_id, item_id, qty) values('" GetCartId() . "', $item_id, $qty)");
    }
    else
    {
        
// This item already exists in the users cart
        // We will update it instead.
        
        
UpdateItem($item_id$qty);
    }
}


// Used to update an item in the cart table.
// simple UPDATE MySQL query on the cart table
// The cookie_id field is used to match the session_id to that product
// so that the quantity is only updated for that one user and product.

function UpdateItem($item_id$qty)
{
    
mysql_query("update cart set qty=$qty where cookie_id = '" GetCartId() . "' and item_id = $item_id");
}[/
small

Reply With Quote
  #5  
Old August 18th, 2003, 10:31 PM
jpipes jpipes is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jpipes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If you take off the error suppression (the @ sign in front of your mysql_query call), does the page return an error when the qty is greater than 1?

Reply With Quote
  #6  
Old August 18th, 2003, 10:50 PM
bmy78 bmy78 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 bmy78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No, I don't get any errors when I remove the @, but if I add another item in the cart when there's already a quantity, it doesn't up the quantity, it just adds that item on a different line. I can have a qty of 3 on the 1st line and a qty of 1 on the 2nd.

Reply With Quote
  #7  
Old August 18th, 2003, 10:58 PM
jpipes jpipes is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jpipes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Does the cart table have a primary key on cookie_id and item_id? If it does, I don't see how you can insert 2 lines into the table with the same cookie_id and item_id, but different quantities...

Check the table to see that the primary key exists...

Also, your update_item function is not incrementing the qty by the qty passed to it, but simply replacing the quantity that's in the table already. Is that the desired functionality? If not, change the SQL to read UPDATE cart SET qty=qty + $qty WHERE cookie_id = " . GetCartID() . " and item_id = $item_id

HTH

Reply With Quote
  #8  
Old January 26th, 2004, 07:59 AM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi

i also had the same problem.
what you have sugessted has fixed it a bit.
what happens when i add item from product.php, it increases the quantity. but when i try to change the quantity from cart.php using the combo box, this doesnt work.

thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > shopping cart problem (newbie)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT