使用复选框问题显示数据库中的值

时间:2013-09-12 22:29:13

标签: php mysql database checkbox

我有一个关于使用复选框显示数据库数据的问题。我能够显示他们不止一次显示的内容。我只需要它们一次显示。如果相同的东西有更多的结果,那么我应该只显示其中一个。如果我选择了多个复选框,我会得到类似这样的内容

ISO 27005:2008
NIST SP 800 - 30
OCTAVE
CobIT
CRAMM
FMEA
FRAP
EBIOS
RuSecure
ALE
Cobra
MEHARI
ISO 27005:2008
NIST SP 800 - 30
OCTAVE
CobIT
CRAMM
FMEA
FRAP
EBIOS
RuSecure
ALE
Cobra
MEHARI
ISO 27005:2008
NIST SP 800 - 30
OCTAVE
CobIT
CRAMM
FMEA
FRAP
EBIOS
RuSecure
ALE
Cobra
MEHARI

而且我只需要一次结果不是相同的结果。我需要某种过滤器或其他东西来比较列出的所有内容,只选择一个实例。每个显示结果都针对所选的每个复选框。如果我选择其中的2个,那么每个将显示2个结果,我只想要一个组合结果,如下所示:

ISO 27005:2008
NIST SP 800 - 30
OCTAVE
CobIT
CRAMM
FMEA
FRAP
EBIOS
RuSecure
ALE
Cobra
MEHARI  

或符合表格条件的任何其他组合 有关删除重复值的任何帮助或建议? 我是初学者,对我的语言感到抱歉。 Ty
        这就是我所拥有的。

lol.php This is where all the action is. I use if statements to check for checked checkboxes.

     <?php
    if (isset($_POST['submit'])) {
        if (isset($_POST['chkbox1']) || (isset($_POST['chkbox2'])) || (isset($_POST['chkbox3']))) {
            if ((isset($_POST['chkbox1']) && (isset($_POST['chkbox2'])))) {
                $upit = mysql_query("SELECT ime FROM kriteriji where identifikacija = 1 and analiza = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }
            if ((isset($_POST['chkbox1'])) && (isset($_POST['chkbox3']))) {
                $upit = mysql_query("SELECT ime FROM kriteriji where identifikacija = 1 and evaluacija = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }
            if ((isset($_POST['chkbox2'])) && (isset($_POST['chkbox3']))) {
                $upit = mysql_query("SELECT ime FROM kriteriji where analiza = 1 and evaluacija = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }
            if (isset($_POST['chkbox1'])) {
                $upit = mysql_query("SELECT  ime FROM kriteriji where identifikacija = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }

            if (isset($_POST['chkbox2'])) {
                $upit = mysql_query("SELECT ime FROM kriteriji where analiza = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }
            if (isset($_POST['chkbox3'])) {
                $upit = mysql_query("SELECT ime FROM kriteriji where evaluacija = 1");
                while ($red = mysql_fetch_array($upit)) {
                    $cijena = $red['ime'];
                    echo $cijena . "<br>";
                }
            }
        } else {
            $upit = mysql_query("SELECT ime FROM kriteriji where identifikacija = 1 and analiza = 1 and evaluacija = 1");
            while ($red = mysql_fetch_array($upit)) {
                $cijena = $red['ime'];
                echo $cijena . "<br>";
            }
        }
    }
    ?>

html file

    <!doctype html>
<html>
    <head>
        <?php
//spajanje na server
        $spajanje = mysql_connect("localhost", "root", "") or die("Nije se mouće povezati na server :("); //spajanje na server
//spajanje na bazu
        $baza = mysql_select_db("magistarski") or die("Nije se moguće povezati na bazu podataka :(");
        ?>
        <meta charset="utf-8">
        <title>Magistarski</title>
    </head>

    <body>


        <form action="lol.php" method="post">
            <p>
            <table width="766" height="418" border="0">
                <caption>
                    <h1>Choose...</h1>
                </caption>
                <tr>
                    <td width="203" height="203"><h3 align="left"><em>Risk Assessment</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox1" value="risk_id" />
                                Risk Identification<br>
                                <input type="checkbox" name="chkbox2" value="risk_an" />Risk Analysis<br>
                                <input type="checkbox" name="chkbox3" value="risk_eval" />Risk Evaluation<br>
                            </em></div></td>
                    <td width="194"><h3 align="left"><em>Risk management</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox4" value="risk_ass" />
                                Risk Assessment<br>
                                <input type="checkbox" name="chkbox5" value="risk_acc" />Risk Acceptance<br>
                                <input type="checkbox" name="chkbox6" value="risk_com" />Risk Communication<br>
                            </em></div></td>
                    <td width="212"><h3 align="justify"><em>Price</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox7" value="free" />
                                Free<br>
                                <input type="checkbox" name="chkbox8" value="notfree" />Not Free<br>
                            </em></div></td>
                </tr>
                <tr>
                    <td height="181"><h3 align="justify"><em>Organization</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox9" value="sme" />SME<br>
                                <input type="checkbox" name="chkbox10" value="large" />Large<br>
                                <input type="checkbox" name="chkbox11" value="goverment" />Goverment<br>
                                <input type="checkbox" name="chkbox12" value="profit" />Profit<br>
                                <input type="checkbox" name="chkbox13" value="nonprofit" />Non - profit<br>
                            </em></div></td>
                    <td><h3 align="justify"><em>Skills</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox14" value="basic" />Basic<br>
                                <input type="checkbox" name="chkbox15" value="standard" />Standard<br>
                                <input type="checkbox" name="chkbox16" value="specialist" />Specialist<br>
                            </em></div></td>
                    <td><h3 align="justify"><em>Documented</em></h3>
                        <div align="justify"><em>
                                <input type="checkbox" name="chkbox17" value="low" />
                                Low<br>
                                <input type="checkbox" name="chkbox18" value="good" />Good<br>
                                <input type="checkbox" name="chkbox19" value="high" />High<br>
                            </em></div></td>
                </tr>
            </table>

        </p>
        <div>
            <input type="submit" name="submit" value="Show" />
        </div>
    </form>
      </body>
       </html>

1 个答案:

答案 0 :(得分:0)

如果我说得对,那么通过使用'if'表达式检查发送的数据,你做错了。 我认为您应首先获取所有必填字段,然后撰写查询。尝试这样的事情:

if (isset($_POST['gumbposlan'])) {

    $whereString = '';

    if (isset($_POST['chkbox'])){
        foreach ($_POST['chkbox'] as $value) {
            if (empty($whereString))
                $whereString .= "where `$value` = 1 ";
            else
                $whereString .= "and where `$value` = 1 ";
        }
    }

    $query = "SELECT DISTINCT `ime` FROM `kriteriji` $whereString";

    $upit = mysql_query($query);
    while ($red = mysql_fetch_array($upit)) {
       $cijena = $red['ime'];
       if ($cijena < 1)
         echo $cijena . "<br>";
    }
}