0

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

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>
4

1 回答 1

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>";
    }
}
于 2013-09-12T23:20:23.437 回答