带有可选字段的PHP MySQL搜索

带有可选字段的PHP MySQL搜索,php,sql,search,Php,Sql,Search,我正在尝试创建一个表单,用户可以在其中输入信息,然后搜索与此查询匹配的表。我希望输入的信息是可选的,并非所有字段都需要填写。第一个“如果”仅适用于楼层和公寓,但在查询中添加“状态”后,无法获得第二个“如果” 我知道代码中的安全问题,但只想在内部服务器上使用它作为我自己的指南 <?php // if there is a value in the floor and flatno field then do this: if (!empty($_POST['Floor']) &a

我正在尝试创建一个表单,用户可以在其中输入信息,然后搜索与此查询匹配的表。我希望输入的信息是可选的,并非所有字段都需要填写。第一个“如果”仅适用于楼层和公寓,但在查询中添加“状态”后,无法获得第二个“如果”

我知道代码中的安全问题,但只想在内部服务器上使用它作为我自己的指南

<?php


// if there is a value in the floor and flatno field then do this:
    if (!empty($_POST['Floor']) && (!empty($_POST['flatno']))) 
{
    require 'connectdb.php';
    $result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]'");
}   

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

include( "searchoutputform.php" );
  }

// second if - if there is something in the floor, flatno and status field then do this:

 elseif (!empty($_POST['Floor']) && (!empty($_POST['flatno'] && (!empty($_POST['status'])))))
{
    require 'connectdb.php';
    $result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]' AND status='$_POST[status]'");
}   

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

include( "searchoutputform.php" );
  }
endif;

// CLOSE CONNECTION TO DATABASE 
mysqli_close($con);
?>
更新:我已经删除了所有的连接请求,所以现在只有1个!我得到以下错误:

分析错误:语法错误,意外的T_BOOLEAN_,预期“这是基于第二个if

暗示

如果这两个值都存在,您的第一个if将始终执行,那么您的elseif将不会执行。如果需要,只需将您的elseif更改为另一个

if (!empty($_POST['Floor']) && !empty($_POST['flatno'] && !empty($_POST['status']))
{
//...
}   
为什么会这样?原因如下

   if(1==1 && 2==2)
   {
      echo "first if";
   }
   elseif(1==1 && 2==2 && 3==3)
   {
     echo "this one will not execute even though the condition is true, due to elseif";
   }
   if(1==1 && 2==2 && 3==3)
   {
     echo "Now this one will work";
   }
这是一个基本的修复方法,但您应该简化逻辑并构建查询,然后在存在值的情况下向其追加值


还有一些额外的括号,还有一些额外的include:

需要多少次“connectdb.php”;自始至终请,基本的调试取决于你,而不是我们。太多的括号如果你添加了一个更新,说你已经修改了代码,请也修改我们可以看到的代码,否则它是相当混乱的!