How would I do this? I’m just trying to do a basic subtraction in my SQL Syntax. Need to subtract a PHP held value. Should be easy, but my syntax is off. Help!
$valueB = 5;
$sql = mysql_query(“UPDATE values SET valueA = valueA – ‘$valueB’”);
Try setting valueA to a variable, and doing the math in code. Then write the result to the database.
Remove the quotes around $valueB in your query. If you’re doing math on numbers, you don’t need them:
… mysql_query(“UPDATE values SET valueA = valueA – $valueB”);
useful tip – if $value comes from elsewhere and you’re not sure if it is numeric or non-empty, do this before query:
$valueB += 0;
if there is only 1 value in the sql table and if you dont escape from sytnax errors with anyway. usethis code
$valueB = 5; $valueA = mysql_query (“select valueA from values”); $valueA = mysql_result($valueA,”valueA”,0); $valueA = $valueA – $valueB; $sql = mysql_query(“UPDATE values SET valueA = valueA – ‘$valueB’”);
Name required
Mail (will not be published) required
Website
3 Comments