Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

substract two vars and add result to mysql database

I have the result of two request, that contains numbers decimal 8,2.

I try to make a substraction of this var and to add result to databse.

Usualy I do like that $var1-$var2

So I have the following request:

$requete= 'UPDATE `decompte` SET
principal_s         = '.$pay['principal_s']-$donnees2['principal_s'].',
clause_penale_s     = '.$pay['clause_penale_s']-$donnees2['clause_penale_s'].',
domages_interets_s  = '.$pay['domages_interets_s']-$donnees2['domages_interets_s'].',
art700_cpc_s        = '.$pay['art700_cpc_s']-$donnees2['art_700_cpc_s'].',
art475_1_cpp_s      = '.$pay['art475_1_cpp_s']-$donnees2['art475_1_cpp_s'].',
art_441_6_cc_s      = '.$pay['art_441_6_cc_s']-$donnees2['art_441_6_cc_s'].',
frais_ar_s          = '.$pay['frais_ar_s']-$donnees2['frais_ar_s'].',
agios_s             = '.$pay['agios_s']-$donnees2['agios_s'].',
depens_s            = '.$pay['depens_s']-$donnees2['depens_s'].',
frais_execution_s   = '.$pay['frais_execution_s']-$donnees2['frais_execution_s'].',
contrib_aid_juridiq_s   = '.$pay['contrib_aid_juridiq_s']-$donnees2['contrib_aid_juridiq_s'].',
frais_greffe_s          = '.$pay['frais_greffe_s']-$donnees2['frais_greffe_s'].'';

mysql_query($requete) or die(__LINE__.mysql_error().$requete); 

The trouble is that it display to me a mistake, it says:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-5000' at line 1-5000.

I'm a bit lost I really do not see what happened there.

anykind of help or advice will be much appreciated

Upvotes: 0

Views: 63

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Put all the subtraction inside brackets. Eg:

principal_s         = '.($pay['principal_s']-$donnees2['principal_s']).',
clause_penale_s     = '.($pay['clause_penale_s']-$donnees2['clause_penale_s']).',

PHP first executes the concatenation, then it evaluates the arithmetic operation!

Upvotes: 3

andrewsi
andrewsi

Reputation: 10732

It's an interesting issue with PHP.

The concatenation operator is evaluated before the subtraction operator.

If you wrap your lines in brackets, it will work:

principal_s         = '.($pay['principal_s']-$donnees2['principal_s']).',

This issue caused me a couple of hours of stress a month or so back before I figured out what was causing it.

Upvotes: 1

Related Questions