Jump to content

loop on checkboxes


mrMarcus

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.