duanping3587 2014-05-02 10:41
浏览 61
已采纳

PHP正在操作所有IF函数而不是完全匹配

I am currently writing a search form in PHP for an SQL query. The user can enter information in any field (aslong as one has information in). Once the form is submitted the mySQL table is searched based on the criteria that they submitted.

The problem I am having is that each IF statement is running not just the one that matches exactly, e.g. if I complete all 3 criteria I get a search for the first criteria, then the second and first and then again for the first second and third.

Any help is appreciated. (This is only for internal use so I know about the SQL security etc but it not important at this point).

<?php

require 'connectdb.php';

// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor'])) 
{
  $result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}   
while($row = mysqli_fetch_array($result))
{
  include("searchoutputform.php");
}

// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor'])) 
{
  $result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}   
while($row = mysqli_fetch_array($result))
{
  include("searchoutputform.php");
}

// if there is ONLY a value in the flat number Field
if (!empty($_POST['flatno'])) 
{
  $result = mysqli_query($con, "SELECT * FROM issuelog WHERE   flatnumber='$_POST[flatno]'");
}   
while($row = mysqli_fetch_array($result))
{
  include("searchoutputform.php");
}

// if there is a value in the floor and flatno field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno']))) 
{
  $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:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno']) && (!empty($_POST['status']))))
{
  $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");
}

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

// CLOSE CONNECTION TO DATABASE 
mysqli_close($con);

?>
  • 写回答

2条回答 默认 最新

  • dpleylxzx47207117 2014-05-02 10:54
    关注

    What I think you mean by this is that if you enter a value for 'Floor' and 'flatno' and 'status' then you are getting back results for all queries. This is because the other two if statements (than the one you want) specify only that the two fields in question need filling, but not that the third must be empty. So all if conditions are met. You need to specify in each that for that condition to run, the others should be empty.

    For example, make your first condition this:

    if ((!empty($_POST['Floor']))&&(empty($_POST['flatno']))&&(empty($_POST['status']))){
    

    And repeat this for all conditions. If I haven't explained this well enough let me know and I'll clarify why

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?