I’ve been trying to develop a code where I have a form to search SQL tables according to date ranges and a checkbox status.
I already have worked out the search query for the dates, but I haven’t been able to make it work with the checkbox. I would like to combine both features in one search but I can’t figure it out.
For ex: Search between “01/01/2012” and “12/31/2012” where status is “DONE! (Checkbox=Checked)“.
Here is the code I’m using to do the search:
The Form
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr>
<td style="text-align:center; padding-top:15px;">
<span>From :</span>
<input type = "date" name = "OLD">
To:
<input type = "date" name = "NEW">
Status:
<input type='checkbox' name='Status' value='DONE'/>
</td>
</tr>
<tr>
<td style="text-align:center; padding-top:15px;">
<button type = "submit" name = "search" value = "Search" class="button orange">Search</button>
<button type = "reset" value = "Clear" class="button orange">Reset</button>
</td>
</tr>
</table>
</form>
The PHP
<?php
if(!isset($_POST['search']))
{
?>
<?php
}
else
{
$OLD = trim($_POST['from']);
$NEW = trim($_POST['to']);
$connection = mysql_pconnect("HOST", "USER", "PASS") or die("Connection failed. ".myslq_error());
mysql_select_db("DATABASENAME") or die("Unable to select db. ".mysql_error());
$query = "SELECT * FROM table WHERE Date >= '$OLD' AND Date <= '$NEW' ORDER BY date ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<table class='table' id='SearchResult' cellspacing='0' cellpadding='0'>";
echo "<tr class='rowa'><b>";
echo "<td class='col1 cell'>Name</td>";
echo "<td class='col2 cell'>Last Name</td>";
echo "</tr>";
echo "</table>";
while($record = mysql_fetch_object($result))
{
echo "<table class='table' id='SearchResult' cellspacing='0' cellpadding='0'>";
echo "<tr class='rowb'>";
echo "<td class='col1 cell'>".$record->Name."</td>";
echo "<td class='col2 cell'>".$record->LastName."</td>";
echo "</tr>";
echo "</table>";
}
}
?>
Who can lead me in the right way to accomplish this?! Thanks a lot!
You can do it like this: