在PHP中使用复选框值

时间:2012-09-18 11:51:09

标签: php mysql sql

我有这个页面(下面的代码),它包含一系列5个复选框,都代表不同的值,我需要用它们来查询数据库。

<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
      <p>Select The <b>Section</b> You Wish To Search In Below...      </p>
      <p>
        <label for="section"></label>
        <label for="section2"></label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
          Functional</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
        Technical</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
        Commercial</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
        Implementation</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
        Innovation</label>
        <br />
      </p>
      <p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
      <p>
        <label for="kword"></label>
        <input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
        <br />
      </p>
      <p>
        <input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
      </p>
    </form>

正如您所看到的,每个都有自己的值,这是用于查询数据库的术语。结果页面上的php查询代码如下

    <?php
          $kword = $_POST["kword"];
          $section = $_POST["SectionSelect"];

          function boldText($text, $kword) {
        return str_ireplace($kword, "<strong>$kword</strong>", $text);
    }


       // Connects to your Database 
     mysql_connect("localhost", "root") or die(mysql_error()); 
     mysql_select_db("test") or die(mysql_error()); 
     mysql_real_escape_string($kword); 
     $data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
    where company.company_id = rfp.company_id
    and rfp.rfp_id = question_keywords.rfp_id
    and question_keywords.section_id = section.section_id
    and section_name like '%$section%'
    and keywords like '%$kword%';") 
     or die(mysql_error());
?>

最终我想要做的是用查询查询数据库,可能有每个复选框值的where子句?例如,我想选择x,其中y喜欢'技术',y喜欢'功能',等等......

任何帮助都会很棒

谢谢

1 个答案:

答案 0 :(得分:0)

将复选框名称设置为数组键,以将它们用作具有所有选中复选框值的数组:

<input type="checkbox" name="SectionSelect[0]" value="Functional" />
<input type="checkbox" name="SectionSelect[1]" value="Technical" />

echo $_POST['SectionSelect'][0]; // should print "Functional"
echo $_POST['SectionSelect'][1]; // should print "Technical"