Jump to content

Table Math


ririe44

Recommended Posts

Okay... I need some math help here...

 

I have a table with columns 'budget' and 'balance'.  When I run my form and hit "submit", I want the value in 'budget' to be added to 'balance'... for the entire table.  Any ideas?

Link to comment
Share on other sites

SELECT (`budget`+`balance`) as BudgBal FROM table_name WHERE condition = somecondition

 

Or an update:

UPDATE table_name SET `balance` = (`balance`+`budget`) WHERE condition = somecondition

 

Or from a variable:

UPDATE table_name SET `balance` = (`balance`+ $budget) WHERE condition = somecondition

Link to comment
Share on other sites

But it would update all 'balances' with the same associated row 'budget' amount, right?

 

Would I code it in like this?  I'm not sure where to make the $add_period_balance work?

 

$query = "INSERT INTO `$tbl_name` (`id`, `period`, `start`, `end`) VALUES ('NULL','".$period."','".$start."','".$end."')";

$add_period_balance = "UPDATE `tbl_name2` SET `balance` = (`balance`+`budget`)";

if (!mysql_query($query));
{
echo "1 Period added.  Make another entry?  <a href='entries.php'>Yes</a>";
}
die(mysql_error());

?>

Link to comment
Share on other sites

I'm inserting a new row into periods, and I'm updating accounts...

 

Basically, when I complete the form to collect the information for a new period, I also need the accounts table to add a value to the account balance for the new period. 

Link to comment
Share on other sites

Okay, let's see...

 

I have an html form called "Add a Period" (basically just to add a pay period into a personal finance workbook)... the form asks for the period title, period start date, and period end date...

 

Period Title:  _________  Period Start: __________  Period End: _____________

Submit btn

 

I also have an accounts table, which is basically a budget.  Each account, or budget item (such as mortgage, groceries, etc) has a pay period spending limit... the budget.  I am calling these budget items accounts because I plan to have the account maintain a balance...  So, every new pay period which is started, I want to add the budgeted amount to the account balance... 

 

So, let's say groceries budget is $150, and there's an account balance of $23 when I start my new pay period.  Once I enter my info into the form above, I hit submit.  It send the information to the periods table to generate the new row in periods.  Upon submit, I'd also like it to add $150 to the current $23 in the grocery  account. 

 

So, after I've clicked submit, I should now have a new pay period, and all my accounts (budget items) should have an increase by the budgeted amount.

 

I hope this makes sense now.

 

So, you can see below the $query is inserting a new row into my periods table... and I was hoping to get $add_period_balance to add the budgeted amount to the balance category of my accounts table...

 

$query = "INSERT INTO `$tbl_name` (`id`, `period`, `start`, `end`) VALUES ('NULL','".$period."','".$start."','".$end."')";

$add_period_balance = "UPDATE `tbl_name2` SET `balance` = (`balance`+`budget`)";

if (!mysql_query($query));
{
echo "1 Period added.  Make another entry?  <a href='entries.php'>Yes</a>";
}
die(mysql_error());

?>

 

 

Link to comment
Share on other sites

Sorry to be a pain, but can you lay out the table structures for each table? I guess I'm not understanding. Maybe someone else understands.

 

So you're inserting a row into periods. It has id, period, start and end fields. You're not inserting anything into accounts, so I don't see where balance and budget is coming from. Maybe the table structures will help. I'm not sure.

 

Thanks!

Link to comment
Share on other sites

accounts has the following columns:

id, category, sub_category, budget, and balance...

 

for example...

 

id, cat, sub, budget, balance

1, food, groceries, 150, 23

2. home, electricity, 30, 12

etc.

 

I want to insert the new row into table periods, and add the budget amount to balance so it would end up like this...

 

id, cat, sub, budget, balance

1, food, groceries, 150, 173

2. home, electricity, 30, 42

etc.

 

Link to comment
Share on other sites

So, how would I modify this to do what I want?

 

$query = "INSERT INTO `$tbl_name` (`id`, `period`, `start`, `end`) VALUES ('NULL','".$period."','".$start."','".$end."')";

$add_period_balance = "UPDATE `tbl_name2` SET `balance` = (`balance`+`budget`)";

if (!mysql_query($query));
{
echo "1 Period added.  Make another entry?  <a href='entries.php'>Yes</a>";
}
die(mysql_error());

?>

Link to comment
Share on other sites

mysql_query($add_period_balance);

?

 

Make sure you ran the SELECT statement above to verify the data you get back are correct. Better yet, run this:

 

SELECT `balance`, `budget`, `balance` + `budget` AS `bal` FROM `accounts`

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.