Forum Moderators: coopster

Message Too Old, No Replies

Check result exists between two dates

between two dates

         

zorro

12:41 am on Jan 21, 2011 (gmt 0)

10+ Year Member



I am trying to display a link if a result exists between two date ranges in a MySQL table but not having much luck.

Code I have so far...

$sql="SELECT * FROM ".$TABLES["enquiries"]." WHERE id='".$_REQUEST["id"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$ENQ=mysql_fetch_assoc($sql_result); ?>

<table width="100%">
<tr><td>Enquiries for:

<?php if (($ENQ["date"]<='2009-12-31')AND($ENQ["date"]=>'2009-01-01')) { ?><a href="link to somewhere">2009</a> <?php } ?>

<?php if (($ENQ["date"]<='2010-12-31')AND($ENQ["date"]=>'2010-01-01')) { ?><a href="link to somewhere">2010</a> <?php } ?>

<?php if (($ENQ["date"]<='2011-12-31')AND($ENQ["date"]=>'2011-01-01')) { ?><a href="link to somewhere">2011</a> <?php } ?>

</td></tr>

PROBLEM IS THAT IT IS ONLY SHOWING THE 2009 LINK!
ANY HELP APPRECIATED

Matthew1980

9:10 am on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Zorro,

First thing that sticks out to me is this:-

$sql="SELECT * FROM ".$TABLES["enquiries"]." WHERE id='".$_REQUEST["id"]."'";

I think that having the id quoted (I assume that it is numerical) is not the best tactic to have, and as your accessing the $_REQUEST array, this is also not advisable; I assume that this is going to be either a $_POST or $_GET - but as your doing this directly in an sql statement, this is leaving you open to problems.

Sanitise the incoming data - and leave the quotes off it the 'id' value is numerical:-

$sql = "SELECT * FROM `".$TABLES['enquiries']."` WHERE `id` = ".strip_tags($_POST['id'])."' ";

Then at least your sql string is being set Ok, I personally would then echo that to screen so that you can see as it is populated as you intend it to be.

And it will only show 2009's link, you need to loop through the returned results from the database query before so that you can display each one in turn.

Also, I guess as there will be an issue with the way that you have your date formats in the sql table, you need to make sure that the formats match, else this won't work the way you expect it too.

Hopefully that makes sense.

Cheers,
MRb