This topic is locked
[SOLVED]

 updating tables with calculations

3/28/2011 9:21:10 AM
PHPRunner General questions
R
rlee6087 author

I have a simple little system, or it started out that way. I have two table inventory and parts, when I select an item from parts.units I would like the inventory.quantity_on_hand table field to be decremented by the value entered into the parts.units table. I tried a trigger but that doesnt appear towork. any help would be greatly appreaciated, thank you.

Sergey Kornilov admin 3/28/2011

I guess you need to use BeforeAdd event of Parts table.
Here is the sample code:

$sql="update inventory set quantity_on_hand = quantity_on_hand + " . $values["units"] . " where inventory_id = " . $values["inventory_id"];

CustomQuery($sql);


Make sure inventory_id is replaced with the actual inventory table key column name.

R
rlee6087 author 3/29/2011

Ok, here is the code I added, the correct field name in inventory is quanity_on_hand, yes I know spelled incorrect however, to late to change now, everything points to that name. Anyway here is the code
$sql="update inventory set quanity_on_hand = quanity_on_hand - " . $values["units"] . " where part_no = " . $values["part_no"];
when I put this in the parts page before record added, i get the following error.
php error happened

error type 256

You have an error in sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near : at line 1

. url localhost/facman/parts_add.php

error line 36

sql query update inventory set quanity_on_hand = quanity_on_hand - 2 where part_no =
2 is the variable number added in the parts.untis table to be deducted from the inventory.quanity_on_hand table.
please advise. thank you

Sergey Kornilov admin 3/29/2011

It looks like $values["part_no"] is empty. Make sure field name spelled properly. Another thing - if part_no is a key column use $keys["part_no"] instead.

R
rlee6087 author 3/29/2011

Tried, the keys and triple checked spelling, same thing. I took the where clause off and it didnt give any errors however it, deleted that amount entered from every row of the quanitiy_on_hand column. So it appears to be something in the where clause.

Sergey Kornilov admin 3/29/2011

I would suggest to post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

R
rlee6087 author 3/30/2011

Thank you so much Jane, it works perfectly.
$sql="update inventory set quanity_on_hand = quanity_on_hand - " .

$values["units"].

" where Part_no = " . $values["Part_No"] ;

CustomQuery($sql);