Solved

MySQL Query by date range

Posted on 2011-03-08
19
509 Views
Last Modified: 2012-05-11
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
Comment
Question by:axessJosh
  • 9
  • 9
19 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35074022
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
 
LVL 2

Author Comment

by:axessJosh
ID: 35074122
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 250 total points
ID: 35074356
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 41

Expert Comment

by:Sharath
ID: 35074375
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
 
LVL 2

Author Comment

by:axessJosh
ID: 35074383
In both examples would i change yyyy-mm-dd to the form variables or leave it as is?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35074457
Change it to the form values always. The other stuff is just placeholder data
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100501
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100556
Just have the form field use dateID as the field name and on post, it will submit $_POST['dateID']
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100590
it is sending the variable data to the URL, how can i pass just to the page?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100603
<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
 
LVL 2

Author Comment

by:axessJosh
ID: 35100621
should i have anything for <form action="" >
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100638
You don't have to set an action.  Forms will post to themselves in absence of an action parameter.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100647
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100693
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
 
LVL 2

Author Comment

by:axessJosh
ID: 35100722
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100829
Try putting action="" back in the form tag and see if that fixes it.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100873
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35101097
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
 
LVL 2

Author Comment

by:axessJosh
ID: 35109007
I tried changing to both variations (GET and POST) and still get the same issue.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mcrypt_create_iv() is deprecated 4 399
Complex MySQL Query 2 38
SQL querys that gives me from one table into another. 2 38
Mysql how to execute a commands file 5 30
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question