Jump to content

Setting a Date variable to use in MySQL QUERY


kilnakorr

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?

Link to comment
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.

Link to comment
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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.