Display Date Range Results

Hi All,

I am having an issues with an SQL query and I can’t quite work out how to resolve it.

I have searched Google for the answer but can’t seem to find the solution there either.

Basically I have a database and in one of the tables it has a start date column and an end date column.

I have a form that you input the start date and end date range you want to search the database for and display the results.

My issues is with getting the dates that are inputted on the form to display on the SQL results page.

I have tried the following code:

$sql = “SELECT * FROM table
WHERE Start >= ‘2020-01-01’ AND End <= ‘2020-02-02’
Order By colour DESC”;

and this works for the given dates specified above.

I have also tried changing it to the following but this didn’t work

$sql = “SELECT * FROM table
WHERE Start >= ‘$start_date’ AND End <= ‘$end_date’
Order By color DESC”;

Thank you in advanced for your help.

It’s impossible to say for sure without knowing the value of $start_date and $end_date. It may be they’re the wrong types; PHP would expect a string here. You can find out what type you’re dealing with by using var_dump($start_date, $end_date); to print the values out.

It’s also worth noting that sticking values into an SQL statement like this will make your code insecure; look into using prepared statements to prevent this. Here and here are good resources for learning about prepared statements, using the preferred PDO system. The rest of these sites are useful for learning the rest of PHP as well.

Thank you for your reply.

I am getting NULL, NULL when printing out these values.

I am assuming this is where the error is coming from and why no results are being shown.

This is the code from where the dates should be inputted:

form action=“submit.php” method=“post”>

	<label>Select Your Start Date of Stays:</label>
            <input type="date" name="start" id="start_date"; ?>
	<label>Select Your End Date of Stays:</label>
            <input type="date" name="end" id="end_date"; ?>
<?php var_dump($start_date, $end_date); ?>
<p>	    <input type="submit" class="btn btn-info" value="Submit Stay">

These are declared on the logging page as follows:
$_SESSION[“start_date”] = $start_date;
$_SESSION[“end_date”] = $end_date;

Can you see where I am going wrong at from here? Could you point me in the correct direction so I am able to solve this issue?

When you submit a form to PHP, the user’s input is available in the $_POST global array. The array keys will correspond to the name attribute of the input tags in your form, rather than the id attribute you seem to be expecting.

If you var_dump($_POST) in your submit.php code, you should see something like the following:

array (
    [start] => "2020-12-16",
    [end] => "2020-12-31"

You can access these values using $_POST['start'] and $_POST['end']. Your user isn’t guaranteed to fill them out correctly, so you should check that the values are present and look like you expect.

Sponsor our Newsletter | Privacy Policy | Terms of Service