I have a sql table in my database “accounts” whose structure looks like this.
ItemNo CostPrice SellingPrice Discount(10%) Price(INR)
100 $100 $150 $135 Rs.7425
101 $200 $250 $225 Rs.10395
102 $150 $200 $180 Rs.7920
103 $500 $550 $495 Rs.25245
Here each column is dependent on the previous one.
SellingPrice = CostPrice + 50
Discount = SellingPrice + (0.1 * SellingPrice)
Price(INR) = SellingPrice * ($_Conversion_rate_to_Rs)
I need to calculate the SellingPrice and update it in the table.
Then I calculate the Discount and update the table
Then I calculate the Price(INR) and update the table.
I am trying the following code but it doesnt seem to be working.
$i = 0;
foreach($item_no as $item ){
mysql_query("UPDATE accounts SET SellingPrice = CostPrice + 50 WHERE item_no = '$item[$i]'") or die(mysql_error());
++$i;
}
Now after updating the SellingPrice I need to calculate the discount and update the database.
$i = 0;
foreach($item_no as $item ){
mysql_query("UPDATE accounts SET Discount = SellingPrice + (0.1 * SellingPrice) WHERE item_no = '$item[$i]'") or die(mysql_error());
++$i;
}
And so on..
My doubt may be quite simple but I tried a lot and couldn find a correct solution.
Can I know my mistakes and the correct solution ?
EDIT!
The way I edit multiple fields is with a custom update function. The function takes an associative array of “field”=>”new value”. Here’s the function:
You use it like this:
update(“theTable”,array(“username”=>”whateverTheNewNameIs”),array(“userID”=>55));
That updates userID=55 and makes the username something new. You can just expand the second array to update more things.
I think you a problem in your updates and your where clause
I’d make a variable for your new cost price,
In your update you can use $newSellingPrice
Then in your where, you have a typo, you didn’t use the $ to get the item array.
You’ll need to leave the query string if you’re using square bracket notation