Jake
Jake

Reputation: 3350

PHP Refresh Page On Submit, Edit SQL Query

I have a page written in PHP that runs a MySQL query, on all months, and on two specific years. When you go to the page, a table containing the resulting data comes up.

What I'd like to do is, after giving the user this default data, allow them to choose which months/years they'd like to view the data from. So, I need to make their dropdown choice (which defaults to January 2011 through December 2012) affect what data shows up. I'm using WordPress, but this isn't WordPress specific.

So, the query I run now within PHP (with some fields anonymized to protect the innocent) is:

    SELECT * 
    FROM database.table
    Where MonthNum >=1
    AND MonthNum <=12
    AND Year >=2011
    AND Year <=2012

But what I'd like to do is create four input fields as dropdowns in HTML above where the query is run. They would be MonthMin (default 1), MonthMax (default 12), YearMin (default 2011), YearMax (default 2012). The query would thus be:

    SELECT * 
    FROM database.table
    Where MonthNum >= MonthMin
    AND MonthNum <= MonthMax
    AND Year >= YearMin
    AND Year <= YearMax

And because it was pulling from the default dates, the output would be identical to the first query. However, what I would then like to do is allow the user to choose a different set of dates, say setting MonthMin to 2, thus ignoring January, and hit "Submit", and then the page should reload but use the user-defined data in the queries instead of the default.

Don't worry about schooling me on SQL injection, I'm working on that too!

Upvotes: 0

Views: 569

Answers (1)

codemonkey
codemonkey

Reputation: 2665

gotcha. you have a few options. you can "submit" without refreshing the whole page using ajax... or you can pass the selected values with the submit get/post and check for their existence in $_REQUEST when the page loads. if they're passed in, you use the values that are passed in, only if new values don't exist in $_REQUEST when the page loads do you default them.

Upvotes: 1

Related Questions