I had a problem about to calculate between php and mysql. How I can deduct a value from database in mysql , write a code to integrate php and mysql. I had a form that contain a value that will be deduct from database.
value from database - value from form(php)=$result
the $result will be store in a same table in database.
The conclusion is, how I create a code for calculation?
tq
As with most things, there are at least two ways to tackle this problem:
You could retrieve the current value from the database into a PHP variable using a SELECT query, then subtract the value submitted with the form from that PHP variable, and then perform an INSERT or UPDATE query to store the resulting value back into the database.
Alternatively, depending on the database structures involved and the version of MySQL you are running, you may be able to do the whole job using an SQL query. An INSERT or UPDATE query could include a SELECT query that retrieved the existing database value and subtracted the value submitted through the form in order to calculate the new value to be stored in the database. This is a fairly advanced SQL technique, however, and I’d need to know more about your database structure to advise you on what the code would look like.
Before writing the code in assumption, can you show us your table structure of source table from where you will get to subtract and target table where you want to store the result so that we can give some codes for you?
$sql = “UPDATE table SET result = value - " . mysql_real_escape_string($form_val) . " WHERE id=1”;
that’s wrong approach.
mysql_real_escape_string means nothing without quotes.
I’d use intval() instead for this case.
revivalx.
In general you do create an SQL query in PHP. That’s the way PHP interacted with mysql. So, as everyone told you, you have to create an SQL query in PHP. And query logic is up to you.