PHP subtract form mySQL?

Geek Squad

New member
I have a mySQL database with points.. And you can spend them. How can you subtract a value form mySQL?
<?php
session_start();

// Include database information and connectivity
include 'login/db.php';
// We store all our functions in one file
include 'login/functions.php';
$a = $_POST['cred_spend'];
$b = user_info('cred');
$cred = $b - $a;
$c = user_info('username');

$con = mysql_connect("localhost","*EDIT*","*EDIT*");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pippin_prologin", $con);

mysql_query("UPDATE members SET cred = " . $cred . " WHERE username = " . $c . "");

mysql_close($con);

// echo $cred;
echo "Cred: <br>";
echo $cred . "<br>";
echo $b . " - " . $a . " = " . $cred;
?>
 
Yeh, but this doesn't give enough information.

Do you want to display a result on a page but do the subtract on that result?
 
try this:

<?php
session_start();

// Include database information and connectivity
include 'login/db.php';
// We store all our functions in one file
include 'login/functions.php';
$a = $_POST['cred_spend'];
$b = user_info('cred');
$cred = $b - $a;
$c = user_info('username');

$con = mysql_connect("localhost","*EDIT*","*EDI...
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("pippin_prologin", $con);

mysql_query("UPDATE members SET cred = " . $cred-$a . " WHERE username = " . $c . "");

mysql_close($con);

// echo $cred;
echo "Cred: <br>";
echo $cred . "<br>";
echo $b . " - " . $a . " = " . $cred;
?>
 
If you just want to increment or decrement cred column in MySQL, you can execute this statement (assuming $a is positive and you want to decrement by $a):

$sql = "UPDATE members SET cred = cred - $a WHERE username = $c";

mysql_query($sql);
 
Back
Top