2013-11-15 57 views
1

我有一個PHP IF語句,基於IF條件將不同的SQL結果保存在一個PHP變量($ sql)中,但它會基於一個條件(第一個條件)持續返回SQL結果,而不管用戶輸入POST'd值。這個PHP IF語句有什麼問題?

所有SQL語句在單獨進入phpMyAdmin時都會按預期工作(同時將$ 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'"; 
} 

回答

10

試試吧

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

你不檢查你正在分配

1)=將右側的值分配給左邊。在你的情況下,它將總是true因爲你正在分配字符串。

2)但==將只檢查左側和右側值/變量。

3)又===也將同時檢查他們的數據類型,是int,float或string..etc

而且我可以看到,在每個條件的所有查詢是相同的,但改變不大的where條件。所以你最好帶上常見的查詢條件並附上條件。它會可讀可靠。

+0

同樣的故事發生在ELSEIF上。 – briosheje

+1

你是個天才!好吧,和我相比,你是大聲笑。非常感謝! 目前我只是很高興現在能夠正常工作,但我會嘗試並按照最後一段的順序進行操作:) – user2968514

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-時搜索任何東西。

+0

這看起來很整潔,謝謝。我確實嘗試過,但它似乎並不適合我。當兩個選項都發布時,它只返回幾行,而不是像它應該做的那樣返回幾百行。它返回的行是沒有分配帳戶組或其他代碼的帳戶。 – user2968514

1

正如您使用了一個等號,您寫的第一個語句將始終返回true。在運算符之後也不需要內部括號。

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

應該是:

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

如果我寫

if ($var = 'test') {} 

它將始終評估爲true $ var值設置成功並返回。我能想到的唯一情況是,如果你將一個變量作爲值傳遞,並且它不存在或者評估爲false,那麼這種情況不會返回true。

在你的情況下,你需要比較運算符'==',因爲你沒有測試是否可以設置一個值,而是它是一個特定的值。

當你每次也是情理之中的其他地方申報的時間也測試了相同的值:

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

===運算符還檢查類型,並且不會在這種情況下非常有用。 如果您比較一個變種,看它是否驗證爲真布爾那麼它作爲一種非假/空/空值的值爲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 

最後,也不需要使用括號,因爲您不會將條件分組在一起。

希望有所幫助。

+0

您還需要檢查用戶是否真的在$ _POST字段中發佈了信息,因爲在當前狀態下,如果其中一個字段留空,整個腳本將不會執行。 您可以使用!is_empty($ row3)來確保您不會向mysql查詢發送空值。 –

+0

非常感謝您的反饋。 POST'd是從html選擇的,所以總是會發布一些東西,我認爲所有可能出錯的地方都是他們可能會選擇錯誤的選項。 – user2968514