Determining when PHP calls a mySQL query

Greetings. Here is my scenario:

A) I have a variable…
$foo = “name-of-fruit-clicked-by-user” ($foo starts out empty)

B) User selects “Apple” from list of fruits and now…
$foo = “Apple”

That part I’m good with, what I also want to do is on the user selection I want to update a mySQL table so that a single row dynamically updates the fruit selection…

mysql_query(“UPDATE fruits SET fruit_name=’$foo’ WHERE id=‘1’”);

So the net result would be the user could pick “Apple” then “Peach” then “Pear” and the dB would update accordingly.

My problem is I can’t figure out how to put a query line into my PHP page without it running when the page first loads. I’ve messed with trying to put it in a function, in a form action, and in a onclick, but PHP still runs the query before I want it to. (On the page load instead of on the user selection)

Any help with this mystery would be greatly appreciated.

mysql_query(“UPDATE fruits SET fruit_name=’$foo’ WHERE id=‘1’”);

the condition isset($_REQUEST[‘foo’]) will only evaluate to true if data was submitted.

I’d suggest using GET or POST instead of REQUEST.


Sponsor our Newsletter | Privacy Policy | Terms of Service