0

I'm new to PHP and currently coding a system where staff members can record shifts, account dept can print the amounts to be paid etc

I need my table which is displayed to members of the account dept to be query-able with a date range. My code seems to work however it is only able to retrieve single dates, not the date range.

Here is the code:

$shiftdata = mysqli_query($connection, "SELECT * FROM tablename_shift INNER JOIN 
tablename_staff ON tablename_shift.uniqueid = tablename_staff.uniqueid WHERE
shift_date BETWEEN '".$_POST['to_date']."' AND '".$_POST['from_date']."'
ORDER by shiftid ASC")

As the code is working for single-date ranges, my initial thought was that the error might lie in how I'm storing the dates. I originally was storing them as VARCHAR(30) in a format of DD/MM/YY, and have since changed it to DATE in a format of YYYY/MM/DD to be compatible with how MySQL stores dates.

Any help would be appreciated.

6
  • 1
    You're code is extremely vulnerable. Regardless of that, that should work as long as your post variables (which again I'm pointing out should not be put directly in the query without sanitation) are in the correct format. Commented Jan 7, 2014 at 13:37
  • change the data type to date or datetime. Commented Jan 7, 2014 at 13:38
  • How is your table structure now...are you now storing them in a date type field instead of a varchar...are they storing just fine now...and the old values, did you convert them to the new format? Commented Jan 7, 2014 at 13:38
  • If you know how I can improve the vulnerability of my code, please elaborate. Commented Jan 7, 2014 at 13:43
  • That is a widely discussed topic. See prepared statements and sql injection Commented Jan 7, 2014 at 13:44

3 Answers 3

1

Mysql between expects the first date to be smaller i.e. from - to, so try this

$shiftdata = mysqli_query($connection, "SELECT * FROM tablename_shift INNER JOIN 
tablename_staff ON tablename_shift.uniqueid = tablename_staff.uniqueid WHERE
shift_date BETWEEN '".$_POST['from_date']."' AND '".$_POST['to_date']."'
ORDER by shiftid ASC")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked. I had it working with WHERE shift_date > '".$_POST['from_date']."' AND shift_date < '".$_POST['to_date'] but I prefer this way.
0

Presumably the staff select a human readable date ie 07-01-2014, then you need to quiz the database using this date but in the mysql date format.

You will need to change the dates before doing the query, like so.

$startDate = date("Y-m-d", strtotime($_POST['from_date']));
$endDate = date("Y-m-d", strtotime($_POST['to_date']));

$shiftdata = mysqli_query($connection, "SELECT * FROM tablename_shift INNER JOIN
tablename_staff ON tablename_shift.uniqueid = tablename_staff.uniqueid WHERE 
shift_date BETWEEN '".$startDate."' AND '".$endDate."'
ORDER by shiftid ASC")

Your from date needs to be first in the query though!

Also beware of the dreaded mysql injections!

Comments

0
you can try this as well.

$shiftdata = mysqli_query($connection, "SELECT * FROM tablename_shift INNER JOIN 
tablename_staff ON tablename_shift.uniqueid = tablename_staff.uniqueid WHERE
shift_date >= '".$_POST['to_date']."' AND  shift_date <='".$_POST['from_date']."'
ORDER by shiftid ASC")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.