使用多个复选框获得 return MySQL 结果

Use multiple checkboxes to return MySQL results

我正在尝试创建一个允许用户选择多个类别的搜索功能,select 提交,然后 php 将 return mysql 结果匹配 selected 类别。

我想在没有 AJAX 或 jQuery 的情况下执行此操作,但如果需要我会使用它们。我是 PHP 世界的新手,所以我认为我走在正确的轨道上,但希望得到一些帮助来纠正这个问题。

HTML

<form action="filter.php" method="post">
    <p class="searching">Filter by category</p>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Arts/Culture"/><img class="lilIcon" src="img/anc.png"><h5>Arts/Culture</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Seniors"/><img class="lilIcon" src="img/senior.png"><h5>Seniors</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Youth"/><img class="lilIcon" src="img/youth.png"><h5>Youth</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Animals"/><img class="lilIcon" src="img/pet.png"><h5>Animals</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Community"/><img class="lilIcon" src="img/comm.png"><h5>Community</h5></section> 
    <section class="box"><input type="checkbox" name="fill" class="check" value="Crisis Support"/><img class="lilIcon" src="img/cs.png"><h5>Crisis Support</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Environment"/><img class="lilIcon" src="img/leaf.png"><h5>Environment</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Faith Based"/><img class="lilIcon" src="img/pray.png"><h5>Faith Based</h5></section> 
    <section class="box"><input type="checkbox" name="fill" class="check" value="People with Disabilities"/><img class="lilIcon" src="img/chair.png"><h5>People with Disabilities</h5></section>
    <input type="submit" name="submit" id="filter" value="Filter"/>    
</form> 

PHP

<?php
$variable=$_POST['fill'];
$i = 0;
foreach ($variable as $variablename) {
    $i++;
    $variablename[number];
}

$result = "SELECT * FROM opportunity WHERE `Category`=$variablename";
$num_rows = mysql_num_rows($result);
   echo "<div class='holder'>"; 

   if(mysql_num_rows($result) > 0)
   {
       while($results = mysql_fetch_array($result));
   }  

我将提交的值更改为 1

<input type="submit" name="submit" id="filter" value="1"/>   

if (intval($_POST['submit') == 1){
  $count = 0;
  $sql = "SELECT * FROM opportunity WHERE `Category` IN ( ";
  $chk = $_POST['chk'];
  foreach($chk as $value){
    $sql .= "'$value',"
    $count++;
  }
  if ($count > 0){
    $sql = substr($sql,0,-1) . ');';  // remove trailing comma and close

     // your sql and output go here

  }
}

如果选择了青年和动物 $sql 将是:

SELECT * FROM opportunity WHERE `Category` IN ('Youth','Animals');

HTML

<form action="filter.php" method="post">
<p class="searching">Filter by category</p>
<section class="box"><input id="chk1" type="checkbox" name="chk[]" class="check" value="Arts/Culture"/><img class="lilIcon" src="img/anc.png"><h5>Arts/Culture</h5></section>
<section class="box"><input id="chk2" type="checkbox" name="chk[]" class="check" value="Seniors"/><img class="lilIcon" src="img/senior.png"><h5>Seniors</h5></section>
<section class="box"><input id="chk3" type="checkbox" name="chk[]" class="check" value="Youth"/><img class="lilIcon" src="img/youth.png"><h5>Youth</h5></section>
<section class="box"><input id="chk4" type="checkbox" name="chk[]" class="check" value="Animals"/><img class="lilIcon" src="img/pet.png"><h5>Animals</h5></section>
<section class="box"><input id="chk5" type="checkbox" name="chk[]" class="check" value="Community"/><img class="lilIcon" src="img/comm.png"><h5>Community</h5></section> 
<section class="box"><input id="chk6" type="checkbox" name="chk[]" class="check" value="Crisis Support"/><img class="lilIcon" src="img/cs.png"><h5>Crisis Support</h5></section>
<section class="box"><input id="chk7" type="checkbox" name="chk[]" class="check" value="Environment"/><img class="lilIcon" src="img/leaf.png"><h5>Environment</h5></section>
<section class="box"><input id="chk8" type="checkbox" name="chk[]" class="check" value="Faith Based"/><img class="lilIcon" src="img/pray.png"><h5>Faith Based</h5></section> 
<section class="box"><input id="chk9" type="checkbox" name="chk[]" class="check" value="People with Disabilities"/><img class="lilIcon" src="img/chair.png"><h5>People with Disabilities</h5></section>
<input type="submit" name="submit" id="filter" value="1"/>    
</form>