JakeMogul Posted December 14, 2012 Share Posted December 14, 2012 I'm creating a system for compiling orders for my business. The script basically is supposed to compile a summarised list of items to order from our various suppliers. I currently have a script that allows me to add items from a list of products via search of drop down menu to a current list of products that need to be ordered. What I'm trying to do now is make is so that if the same item is added to the order twice, it only takes up one row in the table and therefore in the list and adds the quantities together. My current attempt at the script looks like this... <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database name", $con); $count='1'; $TotalQuantity= '$count + tCurrentOrder(Quantity)'; $sql="INSERT INTO tCurrentOrder (Item, Quantity, Date) VALUES ('$_POST[item]','$count','now()') ON DUPLICATE KEY UPDATE tCurrentOrder (Quantity) VALUES ('$TotalQuantity')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo ("Item Added To Order. <a href=\"http page address">here</a> to continue."); $url = 'http page address'; echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; mysql_close($con); ?> The $totalquantity seems to be the current problem. What should it be? Thank you in advance. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2012 Share Posted December 15, 2012 The syntax definition for an INSERT ... ON DUPLICATE KEY ... query is - INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE] [iNTO] [i]tbl_name[/i] [([i]col_name[/i],...)] {VALUES | VALUE} ({[i]expr[/i] | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE [i]col_name[/i]=[i]expr[/i] [, [i]col_name[/i]=[i]expr[/i]] ... ] For your specific information - INSERT INTO tCurrentOrder (Item, Quantity, Date) VALUES ('{$_POST['Item']}',1,now()) ON DUPLICATE KEY UPDATE Quantity = Quantity + 1 Quote Link to comment Share on other sites More sharing options...
JakeMogul Posted December 15, 2012 Author Share Posted December 15, 2012 Thanks. Very much appreciated. Quote Link to comment Share on other sites More sharing options...
JakeMogul Posted December 15, 2012 Author Share Posted December 15, 2012 But, being a n00b, I've forgotten to involve the ItemID in the tCurrentOrder, so while the code doesn't bring up errors, it doesn't do anything either. My fault. How do I copy the ItemID from the tMasterPriceList to tCurrentOrder? I'm trying to use SELECT INTO but don't know whether it needs to be a separate command to the current INSERT INTO command used above. $ItemID = "INSERT INTO tCurrentOrder (ItemID) SELECT ItemID FROM tMasterPriceList" And then alter the VALUES part of the script to VALUES ($ItemID,'$_POST[item]','$count','now()') Am I on the right lines? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2012 Share Posted December 15, 2012 What's in $_POST['Item'] and the `item` column? If it's the item name/title, you would instead post the item id and use that. There's no reason to pass/store more than the identifier of an item. Quote Link to comment 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.