这个PHP IF声明有什么问题?

use*_*514 1 php mysql if-statement

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

当单独输入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'";
}
Run Code Online (Sandbox Code Playgroud)

Gau*_*164 9

试试吧

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

你没有检查你是否正在分配.

1)=将右侧值分配给left.在你的情况下,总是因为true你正在分配字符串.

2)但是==只检查左侧和右侧值/变量.

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

我可以看到,在每种情况下,所有查询都是相同的,但在where条件下变化很小.所以你更好地采取常见的查询,并在条件中附加where条件.它将是可读的,也可以是可靠的.