1

IF 条件に基づいて、さまざまな SQL 結果を PHP 変数 ($sql) に保存するための PHP IF ステートメントがありますが、ユーザーが何を入力しても、1 つの条件 (最初の条件) に基づいて SQL 結果を返し続けます。 POST された値で。

phpMyAdmin に個別に入力すると ($row3 と $row4 を実際の値に変更しながら)、すべての SQL ステートメントは期待どおりに機能しますが、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

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 に答える