Jump to content

Help With Quantity Addition Script


JakeMogul

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.