如果函数不完全匹配,PHP将执行所有操作

如果函数不完全匹配,PHP将执行所有操作,php,sql,if-statement,Php,Sql,If Statement,我目前正在用PHP编写一个SQL查询的搜索表单。用户可以在任何字段中输入信息(只要其中有信息)。表单提交后,将根据提交的条件搜索mySQL表 我遇到的问题是,每个IF语句运行的不仅仅是完全匹配的语句,例如。 如果我完成了所有3个条件,我会搜索第一个条件,然后搜索第二个和第一个条件,然后再搜索第一个和第三个条件 感谢您的帮助。(这只供内部使用,所以我知道SQL安全性等,但在这一点上并不重要) 我认为您的意思是,如果您输入'Floor'和'flatno'以及'status'的值,那么您将返回所有查询

我目前正在用PHP编写一个SQL查询的搜索表单。用户可以在任何字段中输入信息(只要其中有信息)。表单提交后,将根据提交的条件搜索mySQL表

我遇到的问题是,每个IF语句运行的不仅仅是完全匹配的语句,例如。 如果我完成了所有3个条件,我会搜索第一个条件,然后搜索第二个和第一个条件,然后再搜索第一个和第三个条件

感谢您的帮助。(这只供内部使用,所以我知道SQL安全性等,但在这一点上并不重要)


我认为您的意思是,如果您输入'Floor'和'flatno'以及'status'的值,那么您将返回所有查询的结果。这是因为其他两个if语句(而不是您想要的语句)只指定需要填充的两个字段,而不是第三个字段必须为空。因此,如果条件得到满足,所有这些都是必要的。您需要在每个条件中指定要运行该条件,其他条件应为空

例如,将第一个条件设置为:

if ((!empty($_POST['Floor']))&&(empty($_POST['flatno']))&&(empty($_POST['status']))){
并在所有条件下重复此操作。如果我没有很好地解释这一点,请让我知道,我将澄清为什么要尝试这一点,它将帮助您以及为什么要多次编写相同的while条件,最后只添加一次:
Try this It will help you and why writing the same while condition these many times just add once in the last:
<?php

require 'connectdb.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]'");
}


// 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]'");
}


// 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]'");
}

// 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]'");
}   


 // 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]'");
}   



// if there is ONLY a value in the flat number Field
if (!empty($_POST['status']))
{

    $result = mysqli_query($con,"SELECT * FROM issuelog WHERE   flatnumber='$_POST[status]'");
}



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

include( "searchoutputform.php" );
  }



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

感谢您的帮助,James,我正在考虑尝试这个方法,但是如何从我的PHP表单中传递一个在这种情况下可以工作的空白值。楼层和状态是下拉字段()。我是否将选项的值保留为空(value=“”)?完全正确!根据它是否是字符串、整数等,您可以设置不同的“空”值。请参阅PHP文档:使用外部变量构建SQL语句会使代码容易受到SQL注入攻击。此外,任何带有单引号的输入数据,如“O'Malley”,都会破坏您的查询。了解参数化查询,最好使用PDO模块,以保护您的web应用程序。有很多详细的例子。另见备选方案和危险说明。运行使用外部数据构建的SQL语句就像吃从你家门口找到的配料制成的汤。@Andy Lester+1用于汤的类比值得注意的是,在没有绑定的情况下获取行需要mysqlnd驱动程序,如果OP使用类似phpmyadmin的东西,这可能会导致问题
Try this It will help you and why writing the same while condition these many times just add once in the last:
<?php

require 'connectdb.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]'");
}


// 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]'");
}


// 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]'");
}

// 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]'");
}   


 // 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]'");
}   



// if there is ONLY a value in the flat number Field
if (!empty($_POST['status']))
{

    $result = mysqli_query($con,"SELECT * FROM issuelog WHERE   flatnumber='$_POST[status]'");
}



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

include( "searchoutputform.php" );
  }



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