duanping3587 2014-05-02 02: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 02: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部