Question

I am trying to learn how to make calculations by using the user input subtracting with the amount provided in the database. That amount is under of a specific name. Basically (input) - (data from database)

What i know is i have to take out the data first. So my code is:

"SELECT * FROM Table Where username=" + username;

then make calculation and put the new data into the database. This could be done by using the UPDATE.

example,

"UPDATE Table SET amount='" + amount + "'WHERE username='" + userName + "'";

But i have trouble in making the calculations. The amount wont reduced.

In my recent researched i should make:

int newAmount = nowAmount - amount

??? Unsuccessful by the way.

Update info: im using a webservice just incase if you are wondering

Was it helpful?

Solution

That is simple enough to be written in just a few moments.

First you get the input from the user from the form or what so ever the medium. For example:

int userInput = Request.Form["some_input_name"];

And then get the data from the Database:

"SELECT * FROM Table Where username=" + username;

Now, you need to select the column otherwise you'll get an Exception. Try this:

int dbValue = "";
foreach (var row in variable_of_database) {
  dbValue = row.amount;
}

Now, just subtract it.

int newValue = userInput - dbValue;

Then the same UPDATE method.

There is a typo in your code:

"UPDATE Table SET amount='" + amount + "'WHERE username='" + userName + "'";
                           //  ^^^ You sure?

Try updating it to the new variable name i.e. newValue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top