kilnakorr Posted March 16, 2019 Share Posted March 16, 2019 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 16, 2019 Share Posted March 16, 2019 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. Quote Link to comment Share on other sites More sharing options...
kilnakorr Posted March 16, 2019 Author Share Posted March 16, 2019 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 16, 2019 Share Posted March 16, 2019 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 16, 2019 Share Posted March 16, 2019 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.