Jump to content
kilnakorr

Setting a Date variable to use in MySQL QUERY

Recommended Posts

Hi

I'm having a problem getting a query to work.

I have a simple form with user input for start and end date with format: 2009-03-19 (todays date):

$Startdate = $_POST['date'];

This works well when something is entered into the form, and afterwards using my query:

SELECT COUNT(*) as total FROM mydb WHERE Date BETWEEN '$Startdate' AND '$EndDate' ........

Problem is if user submits the form without entering anything in the date input fields, which makes sense.

I want to check if inputs has been made, and if not set af default date, but can't make it work:

if (isset($_POST['date']) && $_POST['date'] !='') {
$Startdate = $_POST['date'];}
else { $Startdate = '1980-01-01';}

How can I set $Startdate to something that can be used in the query as below doesn't work?

Share this post


Link to post
Share on other sites

If your db is using std. date formats I think that the query should be using yyyymmdd   AND - you s/b checking that the input values are valid dates that are either in that format (not likely) or can be converted to that format using normal date functions of php.

Then - echo the values going into the query before running it so you can assure yourself that you have a valid query.  Echo the query statement too before executing it.  Debug. Debug. Debug.

Share this post


Link to post
Share on other sites

 

1 hour ago, ginerjm said:

If your db is using std. date formats I think that the query should be using yyyymmdd   AND - you s/b checking that the input values are valid dates that are either in that format (not likely) or can be converted to that format using normal date functions of php.

Then - echo the values going into the query before running it so you can assure yourself that you have a valid query.  Echo the query statement too before executing it.  Debug. Debug. Debug.

thanks for the input. I'm very aware that the input right now doesn't validate if it's a correct format.

At the moment, I'm simple wondering why the query won't work i $Startdate is set as  $Startdate = '1980-01-01'  but works fine when it comes from the input?

Share this post


Link to post
Share on other sites

Are you setting a default value for enddate too?

When using BETWEEN A AND B

then A must be <= B.

If no dates are entered just omit the WHERE clause from the query.

Share this post


Link to post
Share on other sites

Does your script have error checking turned on so that you get to see the message from the bad query?  What does the query look like when you echo it out before running it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.