gpt4 book ai didi

php - 这个 PHP IF 语句有什么问题?

转载 作者:行者123 更新时间:2023-11-29 03:52:21 25 4
gpt4 key购买 nike

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

最佳答案

尝试一下

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

你没有检查你正在分配。

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

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

3) === 也会检查它们的数据类型,int,float or string..etc

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

关于php - 这个 PHP IF 语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19999998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com