mrMarcus Posted August 27, 2008 Share Posted August 27, 2008 i have a table which has several fields, and i am trying to query/search that table according to what a user check's off in the form via checkboxes. i just can't seem to get it so that the query is executed properly. each field in the database is setup so that it's value is a 0 or a 1 .. here is what i have so far...where $_GET contains the field names. Form: <form action="" method="GET"> <input type="checkbox" name="a[]" value="field1"> <input type="checkbox" name="a[]" value="field2"> <input type="submit"> </form> PHP: <?php if(isset($_GET['a']) && !empty($_GET['a'])){ foreach($_GET['a'] as $key => $value){ if($value==1) $a[] = "$value='".mysql_escape_string($key)."'"; } $a = implode('=1 AND ', $a); $query = mysql_query("SELECT * FROM tableName WHERE $a=1"); while($row = mysql_fetch_array($query)){ $field1 = $row["fieldName"]; } } i just need each value in the query to be "field1=1 AND field2=1", etc...'cause i don't want to return any fields with a '0' value. right now i'm getting an error on the WHILE loop. any thoughts? Link to comment https://forums.phpfreaks.com/topic/121621-loop-on-checkboxes/ Share on other sites More sharing options...
BlueSkyIS Posted August 27, 2008 Share Posted August 27, 2008 $sql = "SELECT * FROM tableName WHERE $a=1"; $query = mysql_query($sql) or die("Query failed: $sql"); Link to comment https://forums.phpfreaks.com/topic/121621-loop-on-checkboxes/#findComment-627355 Share on other sites More sharing options...
mrMarcus Posted August 28, 2008 Author Share Posted August 28, 2008 thanks for the response. so, with this as my code: <?php if(isset($_GET['a']) && !empty($_GET['a'])){ foreach($_GET['a'] as $key => $value){ if($value==1) $a[] = "$value='".mysql_escape_string($key)."'"; } $a = implode('=1 AND ', $a); $sql = "SELECT * FROM utilities WHERE $a=1"; $query = mysql_query($sql) or die("Query failed: $sql"); } ?> i'm getting "Query Failed". Link to comment https://forums.phpfreaks.com/topic/121621-loop-on-checkboxes/#findComment-627398 Share on other sites More sharing options...
mrMarcus Posted August 28, 2008 Author Share Posted August 28, 2008 got it! thanks for the help:) Link to comment https://forums.phpfreaks.com/topic/121621-loop-on-checkboxes/#findComment-627415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.