PHP Conditions Problem
Posted: Sun May 09, 2010 4:34 pm
I have a webpage in PHP that queries the database in mysql. There are many conditions that I need to check for when the query is submitted. for instance, users will have to select from many options like give or take 3 days etc.
I have used a base query and then set conditions. Every works apart from the condition part of the give or take 3 days. When the condition is executed, it just get ignored and only the base query is excecuted. I am not sure why its not working. Can you please advise me or check the code?.Note the condition part of the duration works as expected, its only the condition part for give or take that doesnt work.
----------------- Code--------------------------------------------------------
$dest = $_GET["destination"]; // Variable for destination is retrieved from form in previous page
$dep= $_GET["departuredate"]; // Variable for departure date is retrieved from form in previous page
$giveortake = $_GET["giveortake"]; // Variable for givr or take is retrieved from form previous page
$dep = mysql_real_escape_string($dep); $dest = mysql_real_escape_string($dest);
//$dep is in this format: dd/mm/yyyy so i will change it to mysql format yyyy-mm-dd
list($day, $month, $year) = split( '[/]', $dep);
$depx = "$year-$month-$day";
echo $depx;// to test it works and it works
//begin output exact match
$sql="SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, price,buyurl, departureDate from holidays where departureDate = '$depx' and destination like '%$dest%' ";
if($duration=='7' || $duration=='3'){ $sql .= ' AND duration='.$duration; }
// up to here the code works fine as expected
if($giveortake=='3') { $sql .=' AND departureDate <='. "'$depx'" .' + INTERVAL '.$giveortake.' DAY';}
$result = mysql_query($sql) or die("Query error: ".mysql_errno().": ".mysql_error());
while ($row = mysql_fetch_array($result))
{
outputProduct($row)
;
}
------------------------end----------------------------------------
Thanks in advance

I have used a base query and then set conditions. Every works apart from the condition part of the give or take 3 days. When the condition is executed, it just get ignored and only the base query is excecuted. I am not sure why its not working. Can you please advise me or check the code?.Note the condition part of the duration works as expected, its only the condition part for give or take that doesnt work.
----------------- Code--------------------------------------------------------
$dest = $_GET["destination"]; // Variable for destination is retrieved from form in previous page
$dep= $_GET["departuredate"]; // Variable for departure date is retrieved from form in previous page
$giveortake = $_GET["giveortake"]; // Variable for givr or take is retrieved from form previous page
$dep = mysql_real_escape_string($dep); $dest = mysql_real_escape_string($dest);
//$dep is in this format: dd/mm/yyyy so i will change it to mysql format yyyy-mm-dd
list($day, $month, $year) = split( '[/]', $dep);
$depx = "$year-$month-$day";
echo $depx;// to test it works and it works
//begin output exact match
$sql="SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, price,buyurl, departureDate from holidays where departureDate = '$depx' and destination like '%$dest%' ";
if($duration=='7' || $duration=='3'){ $sql .= ' AND duration='.$duration; }
// up to here the code works fine as expected
if($giveortake=='3') { $sql .=' AND departureDate <='. "'$depx'" .' + INTERVAL '.$giveortake.' DAY';}
$result = mysql_query($sql) or die("Query error: ".mysql_errno().": ".mysql_error());
while ($row = mysql_fetch_array($result))
{
outputProduct($row)
;
}
------------------------end----------------------------------------
Thanks in advance