Liam
Liam

Reputation: 9855

PHP Minus 2 variables for value

I have a page that when data is posted to it inserts the value into my DB.

Im trying to get a sum of all the values from the DB before hand, then insert the value, and then take the SUM away from the value inserted if this makes sense?

$totalquery = mysql_query("SELECT SUM(bill) FROM `outgoings` WHERE outgoings.user_id = '$uid'") or die(mysql_error());
$totalresult = mysql_fetch_array($totalquery);          
$uid = $_SESSION['oauth_id'];
$id = $_POST['col-id'];
$sanitized_monthly_income = mysql_real_escape_string($_POST['monthly-income']);
mysql_query("INSERT INTO income (id, user_id, monthly_income) VALUES ('$id', '$uid', '$sanitized_monthly_income') ON DUPLICATE KEY UPDATE monthly_income = VALUES(monthly_income)");
echo $sanitized_monthly_income - $totalresult["SUM(bill)"];

This is the error I receive though

Notice: Undefined variable: uid in update_salary.php on line 18

Upvotes: 1

Views: 117

Answers (1)

Mark Byers
Mark Byers

Reputation: 838796

You're using $uid before it is assigned:

 $totalquery = mysql_query("SELECT SUM(bill) FROM `outgoings`
                            WHERE outgoings.user_id = '$uid'") or die(mysql_error());
 #                                                     ^^^^
 # then later ...

 $uid = $_SESSION['oauth_id'];

You probably need to move the assignment above the line where you try to access the value.

Upvotes: 4

Related Questions