954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Equations & Database

Fairly new to php but know enough to be dangerous. I'm really stumped on this:

I have stat numbers already in database linked with an id and also created an add stats page. So when a user is on the add stats page they enter the specific id & new stats.

Questions is, once they click submit I need the new stats to add with stats already in database and show the combined stats on the page.

Hope that made sense, basically adding numbers already in database with new numbers coming in.

Any help would be much appreciate - thanks in advance

Giggaman
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Where is the problem?
You dynamic PHP page displays the database content.
When the user submits new stats, store it to the database first place in your script. Then load the data from the database and display the current database content - including the newly submitted data - in the same script.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

Depends on the stats you need. MySQL can produce basic stats by combining values in certain ways, but complicated stats may be better left to php. Your DB should just store values - try to avoid storing intermediate calculated values. 'Stats' then are calculated on the fly (via query and/or subsequent php functions).

If you need to display updated stats after data input without reloading the page, have a look at Ajax.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Ok have a better understanding, the stats are very simple baseball.

What query would you use to add new stats to the database table & combine them to display.

- Example: id [18] has At Bats: 7 (this is already in the database)

- Add Page: user selects id [18] and enters At Bats: 4 (Post HTML Form) & clicks submit

- What query do you use for addition equation? How do you show At Bats: 11

Sorry if I sound like an idiot, hope your not laughing too much at me. Thanks in advance-

Giggaman
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Does user login? If yes then when user logs in query his current stats into session. Then when he adds then by clicking a form submit, just take submitted value, add to session value and update database!

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
UPDATE table1 SET at_bats = at_bats + $newamount WHERE user_id = $id
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

@Ardav your making sense but have one last question, I have more than one record to add, lets say I have at_bats & hits, would the query look like:

UPDATE table1 SET at_bats = at_bats, hits = hits + $at_bats, $hits WHERE user_id = $id


Not sure how to separate records, yet keep them together. - Again Thanks in advance

Giggaman
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

One assignment for each field:

UPDATE table1 SET at_bats = at_bats + $at_bats, hits = hits + $hits WHERE user_id = $id;
smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

@ smantscheff your a freaking genius!!!

@ ardav your a freaking genius !!!

Thanks for helping a beginner out, bravo!!!

Giggaman
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

mark as solved?

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Yes, my problem has been solved, able to add new input with database and display the updated stats. Thanks again!

Giggaman
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: