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

Posted on 2007-10-03
Last Modified: 2010-03-20
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!")
Question by:RottDoggie
    LVL 39

    Expert Comment

    by:Pratima Pharande
    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 ?
    LVL 142

    Accepted Solution

    this should work better:

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

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now