Hi, I have a code for guestbook viewing and I can see all the entries. I need to know how to have it so a user can enter a date range on the website and the entries show up that are between the range.

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="guestbook"; // Database name 
$tbl_name="visitors"; // Table name 
// Connect to server and select database.
mysql_connect("localhost", "root", "ryubest")or die("cannot connect server "); 
mysql_select_db("guestbook")or die("cannot select DB");
$sql="SELECT * FROM visitors";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
echo "<table width=400 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#CCCCCC>";
echo "<tr>";
echo "<td><table width=400 border=0 cellpadding=3 cellspacing=1 bgcolor=#FFFFFF>";
echo "<tr>";
echo "<td>ID</td>";
echo "<td>:</td>";
echo "<td>". $rows['guestid']." </td>";
echo "</tr>";
echo "<tr>";
echo "<td width=117>First Name</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['fname']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=117>Last Name</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['lname']." </td>";
echo "</tr>";
echo "<tr>";
echo "<td width=117>Visit Date</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['visitdate']." </td></tr>";
echo "<tr>";
echo "<td width=117>City</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['city']." </td></tr>";
echo "<tr>";
echo "<td width=117>Country</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['country']." </td></tr>";
echo "<tr>";
echo "<td width=117>Comments</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['comments']." </td></tr>";
echo "<tr>";
echo "<td width=117>E-mail</td>";
echo "<td width=14>:</td>";
echo "<td width=357>". $rows['email']." </td></tr>";
echo "</table></td>";
echo "</tr>";
echo "</table>";

}
mysql_close();
?>

So you need to filter your SQL statement according to the dates entered. This is done like so:

$sql="SELECT * FROM visitors WHERE date_visited > '$startdate' AND date_visited < '$enddate'";

where date_visited is the date column in your visitors table and $startdate and $enddate are the dates that the user has selected.

This article has been dead for over six months. Start a new discussion instead.