在select语句中有4'和'

时间:2015-04-18 10:40:36

标签: php mysql

我的网站上有一个搜索功能,有4个复选框。然后将它们粘贴到下一页,我想找到符合复选框标准的所有产品。

由于我有4个复选框,我想使用4' ands'但我相信3是最大值(?)

如何解决这个问题,以便搜索所有产品是否匹配?

HTML表单


<div id = "search">
            <form name = search action = "search.php" method = "POST">
            <p class = "big"> Refine Menu </p>
            <hr>
            <input type = "text" name = "search" placeholder = "Search for an item" size = "12">
            <input type = "submit" value = "Go"> 
            <br><br>
            <input type = "checkbox" name = "vegetarian"> Vegetarian 
            <br><input type = "checkbox" name = "vegan"> Vegan 
            <br><input type = "checkbox" name = "coeliac"> Coeliac 
            <br><input type = "checkbox" name = "nutFree"> Nut free 
        </form>
        </div>

PHP


<?php
session_start();
include "connection.php";
if(!isset($_SESSION["username"])){
    header("Location: login.php");
}

if(isset($_POST["search"])){
    $search = $_POST["search"];
}

if(isset($_POST["vegetarian"])){
    $vegetarian = 1;
}
else{
    $vegetarian = NULL;
}

if(isset($_POST["vegan"])){
    $vegan = 1;
}
else{
    $vegan = NULL;
}

if(isset($_POST["coeliac"])){
    $coeliac = 1;
}
else{
    $coeliac = NULL;
}

if(isset($_POST["nutFree"])){
    $nutFree = 1;
}
else{
    $nutFree = NULL;
}


$sql = "SELECT * FROM products WHERE vegan = '$vegan' and nutFree = '$nutFree' and vegetarian = '$vegetarian'  and coeliac = '$coeliac'";
$result = mysqli_query($con, $sql);


while($row = mysqli_fetch_assoc($result)){

    echo $row ["name"];

} 

我尝试了很多不同的东西,但我不知道sql的正确语法。

注意:在我的数据库中,它是否满足要求,它被保存为1或0,这就是为什么我从&#39; on&#39;或者&#39;关闭&#39;

3 个答案:

答案 0 :(得分:1)

您可能会考虑类似以下内容,而不是大型的,不可维护的if语句链,这将根据您在表单中检查的哪些必填字段动态构建查询:

<?php
   $search_fields = array( 'vegetarian', 'vegan', 'nutFree', 'coeliac', ...);
   $ands = array( '1' => '1');

   foreach($search_fields as $req)
   {
      if(isset($_POST[$req]) && $_POST[$req] != '')
      {
        $ands[$req] = "$req = '1'";
      }
   }

   $and_part = implode(" AND ", $ands);
   $query = "select .... from ... WHERE $and_part  ... ";
?>

答案 1 :(得分:0)

我设法解决了我的问题。当我发布这个问题时,我错了,因为我认为我的sql语句不起作用的原因是因为有太多的问题而且我没有看到相反我的sql没有按照我的想法行事这应该。

以下是我将其更改为或已设置值或勾选的复选框,但始终是不是或者。

感谢大家的帮助!

<?php
session_start();
include "connection.php";
if(!isset($_SESSION["username"])){
    header("Location: login.php");
}

if(isset($_POST["search"])){
    $search = $_POST["search"];
}

if(isset($_POST["vegetarian"])){
    $vegetarian = 1;
}
else{
    $vegetarian = "  ";
}

if(isset($_POST["vegan"])){
    $vegan = 1;
}
else{
    $vegan = "  " ;
}

if(isset($_POST["coeliac"])){
    $coeliac = 1;
}
else{
    $coeliac = "  " ;
}

if(isset($_POST["nutFree"])){
    $nutFree = 1;
}
else{
    $nutFree = "  ";
}


$sql = "SELECT * FROM products WHERE (vegan = '$vegan' or vegan = 1 xor 0) and (nutFree = '$nutFree' or nutFree = 1 xor 0) and (vegetarian = '$vegetarian' or vegetarian = 1 xor 0) and (coeliac = '$coeliac' or coeliac = 1 xor 0)";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_assoc($result)){
    echo $row ["name"];
}

答案 2 :(得分:0)

PHP NULL在转换为字符串(SQL查询)时没有意义,它们将评估为空,您的查询将看起来像nutFree = '' and vegetarian = '' and coeliac = ''

如果数据库中的这些字段为0,则必须将变量设置为0

在第二种情况下,如果它们在数据库中为NULL,则必须同时更改查询和定义NULL的方式。

首先,那些字符串包装器应该消失。无论如何你都不需要数字,那些只能包裹字符串:

$sql = "SELECT * FROM products WHERE vegan = $vegan and nutFree = $nutFree and vegetarian = $vegetarian and coeliac = $coeliac";

然后,您不必将变量设置为NULL,而是将其设置为字符串 "NULL"

$nutFree = "NULL";

这将使NULL在SQL查询上显示为预期的。