I'm using check-boxes to alter an SQL query.
The HTML:
<input type="checkbox" name="chapter" value="7" /><label>ch. 7</label><br />
<input type="checkbox" name="chapter" value="13" /><label>ch. 13</label>
The PHP:
//adjust query for chapter
$chap = $_POST['chapter'];
$chapter = "";
if(!empty($chap)){
$N = count($chap);
if($N == 1){
$chapter .= " AND chapter = '".$chap[0]."'";
}else{
$chapter .= " AND (chapter = '".$chap[0]."' OR chapter = '".$chap[1]."')";
}
}
$zquery = "SELECT * FROM records WHERE ".$zipcodes.$chapter;
When the check boxes are left unchecked it works fine. When the chapter 7 box is checked it works fine. The trouble is with that 13 box. When I check that, $chap[]
only returns a value of "1"
instead of "13"
. For example, when I var_dump
$zquery
I get string(3384) "SELECT * FROM records WHERE (party_zip='34683' OR party_zip='34682' ) AND chapter = '1'"
.
Also, $N
is always returning 1
, even if I check both boxes. Is $_POST['chapter']
not an array? Why is it only returning the first character in the string?
Namaste