简体   繁体   中英

Choose results by date filter PHP and MySQL

I have one page (members.php) that post date to another page (results.php). Using the below code, i can successfully get the "to" and "from" variables from the members page.

<?php echo $_POST["to"]; ?>
<?php echo $_POST["from"]; ?> 

My problem now is, how can i create a query (in results.php) in order to show filter results only for the dates that are specified at the above variables? Do i need to create an sql connection and sql query too?

If dates are in YYYY-mm-dd format then you can use it as below otherwise you need to change the format.

You can do as below :

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}

//$from_date = $_POST['from_date'];
//$to_date = $_POST['to_date'];

$from_date = date("Y-m-d",strtotime('06/10/2015'));
$to_date = date("Y-m-d",strtotime('06/16/2015'));//$_POST['to_date'];

$query = "SELECT * FROM table WHERE from_date >= '".$from_date."' AND to_date <= '".$to_date."'";
// Perform Query
$result = mysql_query($query);

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
}

You should connect to database and compare this two dates:

$dateOne = $_POST["from"];
$dateTwo = $_POST["to"];

If bigger date is 0, then you should always make it the highest date possible to successfully query Data:

if(empty($dateTwo))
{
    $dateTwo =  date("31-12-Y");
    //If $dateTwo is null, than it will be 31/12/2015 otherwise query will now work well 
}

Then you should write sql query like this:

"SELECT * FROM `table` where `dateOne` > '$dateOne' and `dateTwo` < '$dateTwo'";

In this case if user wants all dates bigger than dateOne but doesn't check dateTwo, this query will return all data bigger than dateOne and lower than highest date available in current year (In our case it's 31/21/2015).

Its not necessarily creating a new sql connection but just a new query (proposed you have already connected to the database). In my opinion, if you want to display the results between the two dates.

I am not checking for existing or empty values since the dates have been already given.

So:

<?php $start_date = $_POST["from"];
      $end_date   = $_POST["to"]; 

    //sql will be 
    $sql = "SELECT * FROM `your_table` WHERE `the_date_column` BETWEEN {$start_date} AND {$end_date}";
?> 

Please but doing this use mysqli or PDO connection layer and also try to escape the variables to prevent sql injection or better off, use prepared statements.

SELECT * FROM sales WHERE build_date BETWEEN '$start_date' AND '$end_date';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM