Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

MySQL Query by date range

The bulk of my new portal is to query data based on users submissions by date range.  I have all the information in 2 tables in my database.  a table for the church information and another table for the weekly submitted information.  The table setup is:

tblChurch
churchID
churchName
etc.

tblChurchInfo
churchInfoID
weeklyGiving
date
churchID

i can figure out how to write mySQL query to get all the data input by church, and church name, what i'd like is to add a filter where the data could be filtered by date.  any suggestions?
0
axessJosh
Asked:
axessJosh
  • 9
  • 9
1 Solution
 
Jason C. LevineNo oneCommented:
Hi again axessJosh,

How exactly is the workflow going to happen?  The users will submit a date or a date range or do you just need to know how to construct the query?
0
 
axessJoshAuthor Commented:
both.

at the user level, they will login and submit their data and select a date (i.e. this past sunday) for the data.

then;

at the admin level, i need for that data to be queried by the date selected by the admin.  I'd like for them to be able select date ranges and see data trends and changes.  For right now, if i can just get the data queried by date i'd be happy.

make sense?
0
 
Jason C. LevineNo oneCommented:
You need to set an advanced recordset and then write the where clause using the field submitted from the form.  The SQL would look something like this:

SELECT tblChurch.*, tblChurchInfo.* FROM tblChurch INNER JOIN tblChurchInfo ON tblChurch.churchID = tblChurchInfo.churchID WHERE tblChurchInfo.date Between YYYY-MM-DD And YYYY-MM-DD;

The above would select a range of dates if you are using two date fields.  In the Advanced View, you would set these as variables using $_POST['datefieldinformname1'] and $_POST['datefieldinformname2'] as the run-time variables.  To do a single date, the SQL would be:

SELECT tblChurch.*, tblChurchInfo.* FROM tblChurch INNER JOIN tblChurchInfo ON tblChurch.churchID = tblChurchInfo.churchID WHERE tblChurchInfo.date = YYYY-MM-DD;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
SharathData EngineerCommented:
Are you looking for something like this?
select *
  from tblChurch c
  join tblChurchInfo ci on c.churchID = ci.churchID
 where ci.date between date1 and date2

Open in new window

0
 
axessJoshAuthor Commented:
In both examples would i change yyyy-mm-dd to the form variables or leave it as is?
0
 
Jason C. LevineNo oneCommented:
Change it to the form values always. The other stuff is just placeholder data
0
 
axessJoshAuthor Commented:
OK, here is what i landed on that works for my purposes.

SELECT tblchurch.churchID, tblchurch.churchName, tblchurchinfo.churchAttendance, tblchurchinfo.churchSSAttendance, tblchurchinfo.churchResponsibility, tblchurchinfo.dateID, tblchurchinfo.churchID FROM tblchurch JOIN tblchurchinfo ON tblchurch.churchID = tblchurchinfo.churchID WHERE tblchurchinfo.dateID = dateVar

dateVar is a dreamweaver variable i made up and is using $_POST['dateID'] to capture the user's input.

now i need have populated a drop down menu with the potential dates to select using DISTINCT in my SQL statement.

how can i pass that variable to my original SQL statement to retrieve the results by dateVar?
0
 
Jason C. LevineNo oneCommented:
Just have the form field use dateID as the field name and on post, it will submit $_POST['dateID']
0
 
axessJoshAuthor Commented:
it is sending the variable data to the URL, how can i pass just to the page?
0
 
Jason C. LevineNo oneCommented:
<form method="post">

You have to choose...post or get.  Can't have it both ways in the same form.  You might have to use two different forms...
0
 
axessJoshAuthor Commented:
should i have anything for <form action="" >
0
 
Jason C. LevineNo oneCommented:
You don't have to set an action.  Forms will post to themselves in absence of an action parameter.
0
 
axessJoshAuthor Commented:
when I select a field, it tries to send me to a new page.  this is the error code i get as a 404 not found.

http://localhost/district_contentDB/2011-02-27

how can i have the jump menu not send to another page?
0
 
Jason C. LevineNo oneCommented:
Without seeing your code I have no idea, but jump menus by definition try to send the user to the selected value.  So if you built a dynamic jump menu, it's doing what you told it to do.
0
 
axessJoshAuthor Commented:
I have attached the code below.  Currently, i am using a list to populated from a recordset using distinct to pull the date.  I then want that date to be passed and retrieved from a second recordset as the filter to display.

i added a php echo field just to check what data was being passed.  Currently, it shows the same date no matter what.
weeklySummary.php
0
 
Jason C. LevineNo oneCommented:
Try putting action="" back in the form tag and see if that fixes it.
0
 
axessJoshAuthor Commented:
Tried that.

for some reason it doesn't seem like the form variable is being sent.  I am wondering if it is a problem with the dreamweaver variable code in one recordset

$dateVar_rsChurchInfo = "-1";
if (isset($_POST['dateID'])) {
  $dateVar_rsChurchInfo = $_POST['dateID'];
}

and the form is pulling the data from a different recordset?  

seems as if the data I am trying to retrieve is not making a connection that dateID is present.  (i realize that is laymans terms)
0
 
Jason C. LevineNo oneCommented:
It's not recognizing because your recordset code (that I'm looking at from the download) is wrong:

$dateVar_rsChurchInfo = "-1";
if (isset($_GET['dateID'])) {
  $dateVar_rsChurchInfo = $_GET['dateID'];
}

0
 
axessJoshAuthor Commented:
I tried changing to both variations (GET and POST) and still get the same issue.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now