如何检查第一个$ _GET的值并打印或检查第二个并打印,如果值不同?

时间:2018-05-11 11:14:03

标签: php mysqli get

为了澄清标题,这是我的代码。它不起作用 - 我确定这是错的。但我不知道我是否离答案很近或很远。我有一个“任何”选项,我想要显示我的数据库中的所有内容,而不是选择的选项,只显示特定的行。我不知道如何展示前者。谢谢!

   $Interest = $_GET['interestId'];



 $sql = "SELECT * from User WHERE (Interest1 = '$Interest' OR Interest2 = '$Interest' OR Interest3 = '$Interest' OR $Interest = 'Any Interest');";

   $result = mysqli_query($link, $sql);

   $resultCheck = mysqli_num_rows($result);

    if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<p>";
            echo Name . ": ";
            echo $row['Fname'] . " ";
            echo $row['Lname'] . "<br><br>";
            echo Interests . ": ";
            echo $row['Interest1'] . ", ";
            echo $row['Interest2'] . ", ";
            echo $row['Interest3']  . "<br><br>";
            echo Website . ": ";
            echo $row['Website']  . "<br><br>";
            echo Personal_Statement . ": <br><br>";
            echo $row['PersonalStatement'] . "<br><br>";
            echo Contact . ": ";
            echo $row['Phone']  . "<br>";
            echo $row['Email'];
            echo "</p>";
        }
    } else {
        echo "<h2>Drat!</h2> There's currently no one with the interest of $Interest!";
    }

现在它不会为任何选择返回任何内容。

1 个答案:

答案 0 :(得分:0)

因此,如果"Any"SELECT * FROM User WHERE (Interest1 = '$Interest' OR Interest2 = '$Interest' OR Interest3 = '$Interest') OR '$Interest' = 'Any' ,那么根本就没有过滤器?您可以将该逻辑放在查询中。例如,考虑这样的事情:

OR

在此逻辑下,如果变量具有字符串"Any",则最后Any将匹配每条记录。所以你基本上说“如果记录与输入匹配,或者输入是{{1}}”。

此外,这很重要,您的代码全开 SQL注入。这意味着您盲目执行用户发送给您的任何代码。这个答案演示了解决方案逻辑,但还有更多需要做的事情。首先要了解SQL注入的内容here,以及有关如何有意义地阻止它的一些快速信息here