valourous Posted April 26, 2011 Share Posted April 26, 2011 Hey guys, Lovely forum. So I'm building a simple shopping cart from php and mysql. Whenever I update the quantity of the item that I want, I don't want it to duplicate all of the information but instead want it to merge the quantity together. Below is an example of what happens. Note that Test Item has been added to the cart 3 separate times with different quantities (2, 1, and 1). How can I merge this information to show Test Item once with the total quantity? I know it's some kind of merge, but I'm a coding noob. Thanks! Here is some (maybe) relevant information: $insert = "INSERT INTO new_cart (sku, quantity, username) VALUES ('".$ssku."', '".$squantity."', '".$username."')"; mysql_query($insert); } echo "<table>"; $select = "SELECT * FROM new_cart JOIN products ON new_cart.sku = products.sku where '".$username."' = new_cart.username"; What needs to be changed or updated? EDIT: If you need more information, feel free to let me know and I'll post it. Quote Link to comment https://forums.phpfreaks.com/topic/234721-shopping-cart-mergeupdate-information-for-one-product/ Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 you could set up an if function to see if the sku number is already inside of your database...and if it is, grab the quantity value that is already in the database, add the new value onto it, and put your new quantity back into your database Quote Link to comment https://forums.phpfreaks.com/topic/234721-shopping-cart-mergeupdate-information-for-one-product/#findComment-1206203 Share on other sites More sharing options...
valourous Posted April 26, 2011 Author Share Posted April 26, 2011 I know that it needs to be an if statement so that if the SKU already exists, it will simply update the quantity of the item. This is some kind of merge function, correct? If you could show me the code that makes this possible, it would be much appreciated. Let me know if you need more information/code. Quote Link to comment https://forums.phpfreaks.com/topic/234721-shopping-cart-mergeupdate-information-for-one-product/#findComment-1206206 Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 Not a merge function no. Simply a math function. I have told you how to do it. Should be smooth sailing from here Quote Link to comment https://forums.phpfreaks.com/topic/234721-shopping-cart-mergeupdate-information-for-one-product/#findComment-1206253 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2011 Share Posted April 26, 2011 You can do this all with a single query - http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html Quote Link to comment https://forums.phpfreaks.com/topic/234721-shopping-cart-mergeupdate-information-for-one-product/#findComment-1206257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.