Jump to content

Archived

This topic is now archived and is closed to further replies.

adgjm

Adding and Subtracting MySQL w/PHP

Recommended Posts

I have a mysql database table with 4 fields. 1st one is ID, 2nd Name, 3rd is the number 50, 4th is the number 10.

I have a single page called "index.php". On this page, there are 4 buttons and one text field. There is a button that says "+1" and another "-1", with the third being "Subtract from 4th Field", and the fourth saying "Random". The textfield is where you input a name. When you click on the +1, it  searches for the name in the text field, and for it's third field using the number 50, it adds 1 to it. When a user clicks on Random, it takes the number in the 4th field (10) and subtracts the 50 by 10. When a user clicks on "Random", it randomly selects either +1, -1, or the number in the 4th field (10) and adds or subtracts depending on what it is.

How do I do this?

Share this post


Link to post
Share on other sites
This has nothing to do with MySQL...?

Share this post


Link to post
Share on other sites
It does. I do not know how to make multiple connections to SQL and how to affect tables in the way I mentioned in my first post. Please help.

Share this post


Link to post
Share on other sites
If you want to do this in real-time, you'll need some sort of asynchronous engine (e.g. AJAX).  The individual statements (adding one, substracting one, subtract A-B, etc.) shouldn't be an issue.

Share this post


Link to post
Share on other sites
I just need to know the individual statements. This does not need to be done in realtime. I am fine if it refreshes the page after you click on +1 or -1.

Share this post


Link to post
Share on other sites
UPDATE yourTable SET yourField = yourField +1 WHERE name = 'yourName'

Share this post


Link to post
Share on other sites
Say I have a single PHP file that includes all those commands. How exactly does that script know what part to execute whether the +1 or -1? Could you show me an example script that shows how the system knows to execute which queries? I know you have lines of PHP to initiate the connection to MySQL, then the next is your query stored into any variable like $line and it executes it when you store it in a variable. How do you make it so that it executes only certain things if you have say 3 different queries like in my case (+1, -1, Random)?

Share this post


Link to post
Share on other sites
I'm not sure I understand... you'll only be issuing one query at a time, and then refreshing the page (according to what you said earlier).  If you need sample PHP code for executing MySQL statements, I'm sure there are plenty of such examples.

Share this post


Link to post
Share on other sites

×

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.