Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:26:32+00:00 2026-06-09T22:26:32+00:00

I have a sql table in my database accounts whose structure looks like this.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:26:34+00:00Added an answer on June 9, 2026 at 10:26 pm

    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:

    function update($table,$values,$conditions){
    
        $query = "UPDATE `" . $table . "` SET ";
        foreach($values as $key => $value){
            if($key == "password")
                $query .= "`password` = PASSWORD('" . $mysqli->real_escape_string($value) . "'),";
            else if(is_numeric($value))
                $query .= "`" . $mysqli->real_escape_string($key) . "` = " . $mysqli->real_escape_string($value) . ",";
            else
                $query .= "`" . $mysqli->real_escape_string($key) . "` = '" . sanitize($value) . "',";
        }
        $query = substr($query,0,-1);
    
        if(!is_string($conditions)){
            $conditionStr = "";
            foreach($conditions as $key => $value){
    
                if($key == "password")
                    $conditionStr .= "`password` = PASSWORD('" . $mysqli->real_escape_string($value) . "') AND ";
                else if(is_numeric($value))
                    $conditionStr .= "`" . $mysqli->real_escape_string($key) . "` = " . $mysqli->real_escape_string($value) . " AND ";
                else
                    $conditionStr .= "`" . $mysqli->real_escape_string($key) . "` = '" . sanitize($value) . "' AND ";
            }
            $conditionStr .= substr($conditionStr,0,-4);
            $conditions = $conditionStr;
        }
    
        $query .= " WHERE " . $conditions . " ;";
        $mysqli->query($query);
    }
    

    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,

    $newSellingPrice = $item[$i]['sellingPrice'] + 50;
    

    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

    "... WHERE item_no = " . $item[$i] . " ;";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have a SQL database table with the following structure: create table category_value (
I have to pull data from a SQL database table to my DB2 table.
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a table in SQL database: ID Data Value 1 1 0.1 1
I have a SQL database that has a table with a field set to
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have an SQL Server 2008 database with a table that has a column
i have 3 Tables is my SQL Database: First Table: Room: ID <pk>, Roomname
I have a SQL Server database with 500,000 records in table main . There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.