• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

MySQL query: ...WHERE 'fieldname' IN ($string)

Should be quick and easy.  Hasn't been.

I have a form with checkboxes for selecting the states to include in a result set of addresses.  The form and each state checkbox on the seach form is written similar to this:

<form method="GET" action="lookup_results.php">
<input type="checkbox" name="geo_select[]" value="ME" id="ME-Maine">&nbsp;ME-Maine<br>

What I want to do is include a variable in my final query that adds a "geo" criteria.  Here's what I'm using so far:

$fields = implode(',',$_GET[geo_select]) ;
$query_geo = " AND 'State' IN ($fields)" ;

Then I add $query_geo to the end of my query string.  Currently, $query_geo is outputting the following from a sample query:


The search is producing no results...even though there are something like 500 addresses in the 6 states above.

All other parts of the query string work fine.

Arrrgh... (or, "your assistance would be greatly appreciated!")
1 Solution
Pratima PharandeCommented:
Please check after you have added the new query condition will the query constructed is correct ?

try out that query direcly on the database and check whether its working or not ?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should work better:

$fields = implode("','",$_GET[geo_select]) ;
$query_geo = " AND `State` IN ('$fields' )" ;
RottDoggieAuthor Commented:
Thanks both.

Here's where it ended up:

      $fields = implode('","',$_GET[geo_select]) ;
      $fields = stripslashes($fields) ;
      $fields = '"' . $fields . '"' ;
      $query_geo = "AND State IN ($fields)" ;

Slightly different than Angellll post, but it got me on the right track.  

Kudos also to pratima.  I forget that the best way to write/troubleshoot these is to query directly to the database then write the working query into the form/handler.  Saves a lot of time.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now