0

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!

5
  • You've described what you've done and want to do well, but I'm not clear what the problem/question is. Are you having trouble writing the php code that incorporates submitted variable values into the query? Commented Jun 8, 2012 at 16:45
  • Yes, precisely. I know what I want to do, I just don't know how to actually do it. For instance, if I implemented my idea as-is, when the user hits the Submit button, the page would refresh, and as such, the input fields would be reinitialized to their default values, and the query would be run again, but with the default values. How do I 'save' the user's choice? Commented Jun 8, 2012 at 17:01
  • 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. Commented Jun 8, 2012 at 18:46
  • I have been working on it as a GET request, and it seems to be working quite well. I don't have all the functionality yet, but it does seem to be an easy way to save the state and read it back after a user submits data. Thanks! Could you add your response as an answer so I can accept it and give you some points? =) Commented Jun 8, 2012 at 18:53
  • added as answer. glad you've got it working. Commented Jun 8, 2012 at 19:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.