0

I have 2 multiple checkboxes and have problems to display it correct if more than one checkbox is ticked. Only the last checkbox get used for the filter in the Select query.

I was searching a lot in the web, but I don't find someone who has also a problem with this.

Question: How to use SELECT(sql) with multiple checkboxes?

HTML Code:

<aside><h3>Filter:</h3>
    <details open="open">
    <summary><label>Categorie</label></summary>
        <div>
            <label>
            <input  type="checkbox" name="filter_categorie[]" value="dog">
            Dogs
            </label>
        </div>
        <div>
            <label>
            <input  type="checkbox" name="filter_categorie[]" value="fish">
            Fishes
            </label>
        </div>
        <div>
            <label>
            <input  type="checkbox" name="filter_categorie[]" value="other">
            Other
            </label>
        </div>
    </details>
    <details>
    <summary><label>Land</label></summary>
        <div>
            <label>
            <input  type="checkbox" name="filter_country[]" value="germany">
            Germany
            </label>
        </div>
        <div>
            <label>
            <input  type="checkbox" name="filter_country[]" value="austria">
            Austria
            </label>
        </div>
    </details>

    <br><input type='submit' name ='update' value='Update'>

PHP Code:

include("server.inc");
$cxn = mysqli_connect($host,$user,$password,$database) or die ("No connection to Server");
if(isset($_POST['update']) ){
    foreach ($_POST['filter_categorie[]'] as $item){
        $query = "
        SELECT * 
          FROM artikel 
         WHERE Categorie=\"{$_POST['filter_categorie']}\"
";}
$resultat = mysqli_query($cxn,$query) or die ("No results.");
4
  • 1
    You didn't use as $item. You're passing the POST array instead. Commented Dec 12, 2018 at 14:34
  • 1
    This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers Commented Dec 12, 2018 at 14:35
  • @mplungjan, TBH I think they want it something like Categorie in ( ). Commented Dec 12, 2018 at 14:43
  • @NigelRen I am sure a better suggestion was available. OPs version will not work. Commented Dec 12, 2018 at 14:44

1 Answer 1

1

Try something like this (not tested, its just an idea):

include("server.inc");
$cxn = mysqli_connect($host,$user,$password,$database) or die ("No connection to Server");
if(isset($_POST['update']) ){

  $query = 'SELECT * FROM artikel WHERE Categorie IN ('.implode(',', $_POST['filter_categorie']).')';

  $resultat = mysqli_query($cxn,$query) or die ("No results.");
}

So basically use $_POST['filter_categorie'] not $_POST['filter_categorie[]'] and use implode to give all categories as a string in you query.

No need to use foreach, you don't want to run as many queries as many selections of categories in the front end.

Sign up to request clarification or add additional context in comments.

4 Comments

Me too :) at least give the reason
@ivanivan Thank you!
Did not downvote, but the statement should be Categorie IN (x,y,z) instead of Categorie IN "x,y,z".
If anyone uses this solution, you still need to escape it the user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.