1

我有一个 PHP IF 语句,用于根据 IF 条件将不同的 SQL 结果保存在 PHP 变量 ($sql) 中,但无论用户输入什么,它都会仅基于一个条件(第一个条件)返回 SQL 结果在 POST 的值中。

当单独输入 phpMyAdmin 时,所有 SQL 语句都按预期工作(同时将 $row3 和 $row4 更改为存在的实际值),只是没有使用 PHP IF 语句。

谁能看到我在这里做错了什么,如果可能的话,建议我需要做些什么不同的事情?我知道我不是 PHP / MySQL 专家,但我很难过 :(

非常感谢任何帮助或建议。提前致谢。

$row3 = $_POST['groups'];
$row4 = $_POST['othercode-all'];

IF ($row3='-all-' && ($row4='-all-')) 
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'";
}


ELSEif ($row3!='-all-' && ($row4='-all-')) 
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3'";
}


ELSEIF ($row4 != '-all-' && ($row3 = '-all-'))
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND othercode = '$row4'";
}

ELSE
{
$sql ="SELECT   
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4'";
}
4

3 回答 3

9

试试看

if ($row3 == '-all-' && $row4 == '-all-') {
     // Do the stuff
} elseif ($row3 != '-all-' && $row4 == '-all-') {
     // Do another
}   

你没有检查你正在分配。

1)=将右侧的值分配给左侧。在您的情况下,它将始终true是您分配字符串。

2)但==只会检查左侧和右侧的值/变量。

3)并且===还将检查它们的数据类型,无论是int,float还是string..etc

而且我可以看到,在每个条件下,所有查询都是相同的,但在where条件下变化很小。所以最好采用常见的查询,并在条件中附加 where 条件。它也可读且可能可靠。

于 2013-11-15T11:26:10.803 回答
1

当您使用单个等号时,您编写的第一条语句将始终返回 true。&& 运算符之后也不需要内部括号。

IF ($row3='-all-' && ($row4='-all-')) {}

应该:

if ($row3 == '-all-' && $row4 == '-all-') {}

如果我要写

if ($var = 'test') {}

当 $var 的值被成功设置并返回时,它将始终评估为 true。我能想到的唯一不会返回 true 的情况是,如果您将变量作为值传递,并且它不存在或评估为 false。

在您的情况下,您需要比较运算符“==”,因为您不是在测试是否可以设置值,而是在测试它是一个特定的值。

由于您每次都在测试相同的值,因此在其他地方声明它也是有意义的:

$str = '-all-';
if ($row3 == $str && $row4 == $str) { //logic }

=== 运算符也检查类型,在这种情况下不会有用。如果您正在比较一个 var 以查看它是否验证为 TRUE 布尔值,那么它很有用,因为非假/空/空值评估为 TRUE,即使它不是 TRUE 布尔值:

$t1 = 'FALSE';
$t2 = FALSE;

if ($t1 === FALSE)
//evaluates as false as t2 is a string not a boolean
if ($t2 === FALSE)
//evaluates as true

最后,也不需要括号,因为您没有将条件组合在一起。

希望有帮助。

于 2013-11-15T12:02:54.827 回答
0

你也可以试试:

$row3 = ($_POST['groups']=='-all-') ? '%' : $_POST['groups'];
$row4 = ($_POST['othercode-all']=='-all-') ? '%' : $_POST['othercode-all'];

$sql ="SELECT   
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4'";

这样代码就更简单了。您总是使用 where 子句,但是当它的值为 -all- 时搜索任何内容。

于 2013-11-15T11:52:31.723 回答