Old 11-07-2006, 09:15   #1 (permalink)
30equals
Senior Member
 
Join Date: Jan 2006
Posts: 146
php shopping cart probs

posting this on phpbuilder didn't get me any solutions:::

i'm working on a shopping cart, everything works fine except for two things.

-when i click "add to cart" with a certain item twice. it adds another row in the temp. cart in stead of updating the quantity from 1 to 2.
-refreshing the cart page adds the product (i know you can solve this partially through redirection to another page)

so my main problem is updating the quantity when clicking an item more than once.

my add.php code

PHP Code:
<?php 
    session_id
();
    
session_start();
    
    
$qty $_GET['qty'];
    
$prodnum $_GET['prodnum'];
    
$sess session_id();
    
    
    
$query "SELECT * FROM br_carttemp ";
     
$result mysql_query ($query);
     
$num_records = @mysql_num_rows ($result);
     
$row mysql_fetch_array($result);
     
$oldqty $row['quan'];
        print 
$qty
     if (
$num_records 0) {

     
$newqty = ($oldqty $qty);
     
$query "UPDATE br_carttemp SET quan='$newqty' WHERE prodnum='$prodnum'";
     } 
     
     else {
     
$query2 "INSERT INTO br_carttemp (sess, quan, prodnum) VALUES ('$sess', '$qty', '$prodnum')";
     
$result2 mysql_query($query2)
     or die(
mysql_error());
}

    
     include (
"cart.php");


?>



oh and i got this code from someone to update the quantity, but this doesn't even adds the item anymore

Last edited by 30equals : 11-07-2006 at 09:27.
  Reply With Quote
Old 11-07-2006, 10:11   #2 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 836
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
What I would do is utilise the MySQL UPDATE query to change the quantity instead of pulling it out, assigning the old + new value and then updating. Something along the lines of:

UPDATE br_carttemp SET quan = quan + '$qty' WHERE prodnum='$prodnum'

That in theory should get MySQL to take the existing amount and then add the new quantity value to it. It is slightly quicker process that the one you have now. One other thing to note is that you should also define which item you are updating with the 'sess' too otherwise it will update the quantity for any customer/session that is active and has that product in there cart; not what you want to happen:

UPDATE br_carttemp SET quan = quan + '$qty' WHERE sess = '$sess' AND prodnum = '$prodnum'

This rule should also apply when you are checking for the existance of the product in the cart in order to determine whether you do an UPDATE or INSERT:

SELECT * FROM br_carttemp WHERE sess = '$sess' AND prodnum = '$prodnum'

Otherwise your code will not work when there are multiple users/sessions active on your cart system. Any action performed by one customer would also affect all other customers who are shopping at the time.

I've also noticed that you are calling $query2 to perform the UPATE or INSERT action however the UPDATE is assigned to $query (should be $query2 ?).

In order to sort out the refresh issue, I would advise that you redirect the users browser to a URL that simply displays the cart contents after the UPDATE or INSERT action has been performed (such as cart.pl?action=view or something similar). This way when they refresh their browser they are not resubmitting the GET values, which is causing your action to occur twice.

- Mike
  Reply With Quote
Old 11-07-2006, 10:32   #3 (permalink)
30equals
Senior Member
 
Join Date: Jan 2006
Posts: 146
mike!

thanx for the fast reply. i was reading your suggestions and made some adjustments to my queries (the query assignment works like i posted though) and that was enough really.

so stupid. so selecting where sess ='$sess' AND prodnum ='$prodnum'
works already. i can't believe that i missed this.

but i'm gonna try your query with the addition of values in the update part.

thanx again!
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8