基于多个HTML复选框的MYSQL查询

时间:2014-11-10 19:39:44

标签: mysql checkbox

我有一张包含土壤分析的表格,大约有400k行,大约有30列。所有行都有一个Year列,它在1997年到2014年之间显示了一些内容。

我让用户根据每年的几个HTML表单和一个复选框进行MySQL查询。 如果没有填写复选框,则会在表格中搜索每年。 我的目的是让用户能够在复选框的帮助下选择特定的一年或几年。

我已经复制了我现在拥有的东西。 (我已经排除了变量和东西,请原谅瑞典人在这里和那里) HTML表单工作正常。但我还没想出如何使用复选框。我不知道如何在MySQL查询中实现它们。我试过谷歌搜索但这个问题在我的案例中似乎有点具体。 我很高兴听到你对此事的看法。

        <form id="form" action="statisticsToMap.php" method="post">
            <p>______Minimum_____________Maximum______</p>
            <p>pH: <input type="text" name="min-ph" onkeypress="return isNumberKey(event)">  pH: <input type="text" name="max-ph" onkeypress="return isNumberKey(event)"></p>
            <p>P-AL: <input type="text" name="min-p" onkeypress="return isNumberKey(event)">  P-AL: <input type="text" name="max-p" onkeypress="return isNumberKey(event)"></p>
            <p>K-AL: <input type="text" name="min-k" onkeypress="return isNumberKey(event)">  K-AL: <input type="text" name="max-k" onkeypress="return isNumberKey(event)"></p>
            <p>Mg-AL: <input type="text" name="min-mg" onkeypress="return isNumberKey(event)">  Mg-AL: <input type="text" name="max-mg" onkeypress="return isNumberKey(event)"></p>
            <p>Lerhalt: <input type="text" name="min-ler" onkeypress="return isNumberKey(event)">  Lerhalt: <input type="text" name="max-ler" onkeypress="return isNumberKey(event)"></p>
            <p>Sand-Grovmo: <input type="text" name="min-sgrovmo" onkeypress="return isNumberKey(event)">  Sand-Grovmo: <input type="text" name="max-sgrovmo" onkeypress="return isNumberKey(event)"></p>
            <p>Mullhalt: <input type="text" name="min-mull" onkeypress="return isNumberKey(event)">  Mullhalt: <input type="text" name="max-mull" onkeypress="return isNumberKey(event)"></p>
            <p>Klicka i vilka årtal som du vill söka efter: (tomma rutor söker alla årtal)</p>
            <input type="checkbox" name="1997" value="1997"> 1997
            <input type="checkbox" name="1998" value="1998"> 1998
            <input type="checkbox" name="1999" value="1999"> 1999
            <input type="checkbox" name="2000" value="2000"> 2000
            <input type="checkbox" name="2001" value="2001"> 2001<br>
            <input type="checkbox" name="2002" value="2002"> 2002
            <input type="checkbox" name="2003" value="2003"> 2003
            <input type="checkbox" name="2004" value="2004"> 2004
            <input type="checkbox" name="2005" value="2005"> 2005
            <input type="checkbox" name="2006" value="2006"> 2006<br>
            <input type="checkbox" name="2007" value="2007"> 2007
            <input type="checkbox" name="2008" value="2008"> 2008
            <input type="checkbox" name="2009" value="2009"> 2009
            <input type="checkbox" name="2010" value="2010"> 2010
            <input type="checkbox" name="2011" value="2011"> 2011<br>
            <input type="checkbox" name="2012" value="2012"> 2012
            <input type="checkbox" name="2013" value="2013"> 2013
            <input type="checkbox" name="2014" value="2014"> 2014
            <input type="checkbox" name="2015" value="2015"> 2015
            <input type="checkbox" name="2016" value="2016"> 2016<br>
            <p>Begränsa antal resultat:</p> <input type="text" name="limitrows" onkeypress="return isNumberKey(event)"> (Standard är 2000 rader)<br><br>

            <table>
                <tr>
                <input type="submit" value="Sök i databas"onclick="submitForm('')">
                <input type="submit" value="Visa resultat på karta" onclick="submitForm('statisticsToMap.php')">
                </form>
                </tr>
            </table>


 $sql = "SELECT `kundnr`, `Year`, `Provnr`, `pH`, `P_AL`, `P_HCl`, `K_AL`, `K_HCl`, `Mg_AL`,     
        `Cu_HCl`, `K_Mg_kvot`, `Bor`, `Ca_AL`, `Total_lerhalt`, `Sand_grovmo`, `Mullhalt` 

        FROM `analyser`

        WHERE 


            (IFNULL(`pH`,               '0')    BETWEEN $minph      AND $maxph)
        AND (IFNULL(`P_AL`,             '0')    BETWEEN $minpal     AND $maxpal)
        AND (IFNULL(`K_AL`,             '0')    BETWEEN $minkal     AND $maxkal)
        AND (IFNULL(`Mg_AL`,            '0')    BETWEEN $minmg      AND $maxmg)
        AND (IFNULL(`Total_lerhalt`,    '0')    BETWEEN $minler     AND $maxler)
        AND (IFNULL(`Mullhalt`,         '0')    BETWEEN $minmull    AND $maxmull)
        AND (IFNULL(`Sand_grovmo`,      '0')    BETWEEN $minsgrovmo AND $maxsgrovmo)

        LIMIT 0,$limitrows";

/西蒙

1 个答案:

答案 0 :(得分:0)

这不是简单的情况,您将根据年份选择动态构建YEAR IN (..)子句吗?例如。选择2008年,2010年和2012年时:

SELECT * FROM SOIL_DATA WHERE YEAR IN (2008, 2010, 2012)

相关问题