Building an SQL query based on checkboxes

后端 未结 1 439
眼角桃花
眼角桃花 2021-01-29 01:11

Let\'s say I have a form that has 30 checkboxes that correspond to music genres (it submits to a PHP form handler).

I have an artists table that has a genre field. What

相关标签:
1条回答
  • 2021-01-29 01:52

    You would likely want to use IN() in your where clause like this:

    WHERE genreId IN (1,2,3)
    

    Unfortunately, there really isn't a good solution to use this in a parameterized way. You just have to go old-school:

    $query = 'SELECT ... WHERE genreId IN (' . implode(',', $genreId_array) . ')';
    

    I suppose though you could do something like this to build a parametrized approach:

    $query = 'SELECT ... WHERE genreId IN (';
    $array_length = count($genreId_array);
    for ($i = 0; $i < $array_length; $i++) {
        $query .= '?,';
    }
    $query = rtrim(',',$query) . ')';
    
    0 讨论(0)
提交回复
热议问题