我有一个 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'";
}