It's the age-old question of checkboxes. I've got this code:
<?php
require "connect_to_mysql.php";
// Start the SQL string
$sql = "SELECT * FROM post_sec_all_stats WHERE 1=1";
if (isset($_POST ['dura_sch'])) {
$sql .= " AND ";
$sql .= "DURA_SCH = '" .$_POST ['dura_sch']. "'";
}
if (isset($_POST['ctrl_sch'])) {
$sql .= " AND ";
$sql .= "CTRL_SCH = '" .$_POST['ctrl_sch']. "'";
}
if (isset($_POST['total_sts'][0])) {
$sql .= " AND ";
$sql .= "TOTAL_STS < 2000";
}
if (isset($_POST['total_sts'][1])) {
$sql .= " AND ";
$sql .= "TOTAL_STS BETWEEN 2000 AND 20000";
}
if (isset($_POST['total_sts'][2])) {
$sql .= " AND ";
$sql .= "TOTAL_STS > 20000";
}
$sql .= " ORDER BY UNITID ASC";
print_r($sql);
$result = mysql_query($sql) or die("Error processing <strong>query</strong>. ".mysql_error());
$row = mysql_fetch_assoc($result) or die("No rows returned by query");
echo $result;
while($booyah = mysql_fetch_object($result))
{
echo "<tr>";
echo "<td><input type = 'radio' name = 'UNITID' value = '".$booyah->UNITID."'></td>";
echo "<td>$booyah->NAME_SCH</td>";
echo "<td>$booyah->ADD_SCH $booyah->CITY_SCH, $booyah->STATE_SCH</td>";
echo "<td>$booyah->WEB_SCH</td>";
echo "</tr>";
}
?>
and the checkbox form:
<input type="checkbox" value="1" name="total_sts[]"> Small (fewer that 2000)<br />
<input type="checkbox" value="2" name="total_sts[]"> Medium (2,000 - 20,000)<br />
<input type="checkbox" value="3" name="total_sts[]"> Large (more than 20,000)<br />
It's nice and simple, because I'm an idiot, but I can't figure out how to code for searching the checkboxes. If I write it like I have it now, my search results in nothing, because obviously TOTAL_STS can't be both less than 2000, between 2000 and 20000, and over 20000 at the same time. I confirmed this by echoing out the results:
SELECT * FROM post_sec_all_stats WHERE 1=1 AND DURA_SCH = '1' AND CTRL_SCH = '1' AND TOTAL_STS < 2000 AND TOTAL_STS BETWEEN 2000 AND 20000 AND TOTAL_STS > 20000 ORDER BY UNITID ASC
SOOOoooo, if the user checks just one box, it's fine and dandy, but if they check more than one, it screws up the results. Can someone tell me how to fix this? As you can tell, I'm just starting with this whole coding thing.