Jump to content


Photo

Adding and Subtracting MySQL w/PHP


  • Please log in to reply
7 replies to this topic

#1 adgjm

adgjm
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 October 2006 - 09:10 PM

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?

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 October 2006 - 09:34 PM

This has nothing to do with MySQL...?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 adgjm

adgjm
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 October 2006 - 09:55 PM

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.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 October 2006 - 11:53 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 adgjm

adgjm
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 October 2006 - 12:28 AM

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.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 October 2006 - 12:30 AM

UPDATE yourTable SET yourField = yourField +1 WHERE name = 'yourName'
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 adgjm

adgjm
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 October 2006 - 03:35 AM

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)?

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 October 2006 - 03:49 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users