| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
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:
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. |
|
|
|
|
|
#2 (permalink) |
|
Everything is fine.
|
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 |
|
|
|
#3 (permalink) |
|
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! |
|
![]() |