2012-09-01 202 views
0

前三個查詢工作正常,但最後一個不是。第二屆查詢與$status !== ''好嗎工作,但最後一個具有相同$status !== ''不執行查詢任何一個可以幫助我需要幫助mysql查詢

if (isset($_POST['go'])) { 

    $status = $_POST[status]; 
    if ($status == 4){ 
      $status = '' ; 
    } 

    if ($topic == 100 && $subtopic == '' && $status == '') { 
    $queryString = "select * from tblquiz where userid='$_SESSION[numericuserid]' LIMIT $start, $limit" ; 
    $countString = "select * from tblquiz where userid='$_SESSION[numericuserid]'"; 
    } // NOTE : THIS QUERY IS WORKING PERFECT 

     elseif ($topic !== '' && $subtopic !== '' && $status !== '') { 
    $queryString = "select * from tblquiz where qstatus=$status AND qtopic='$topic[topicname]' AND subtopic='$_POST[subtopic]' AND userid='$_SESSION[numericuserid]' LIMIT $start, $limit" ; 
    $countString = "select * from tblquiz where qstatus=$status AND qtopic='$topic[topicname]' AND subtopic='$_POST[subtopic]' AND  userid='$_SESSION[numericuserid]'"; 
     } // NOTE : THIS QUERY IS WORKING PERFECT 

     elseif ($topic !== '' && $subtopic !== '' && $status == '') { 
    $queryString = "select * from tblquiz where qtopic='$topic[topicname]' AND subtopic='$_POST[subtopic]' AND userid='$_SESSION[numericuserid]' LIMIT $start, $limit" ; 
    $countString = "select * from tblquiz where qtopic='$topic[topicname]' AND subtopic='$_POST[subtopic]' AND userid='$_SESSION[numericuserid]'"; 
     } // NOTE : THIS QUERY IS WORKING PERFECT 


     elseif ($topic == 100 && $subtopic == '' && $status !== '') { 
    $queryString = "select * from tblquiz where qstatus= $status AND userid='$_SESSION[numericuserid]' LIMIT $start, $limit" ; 
    $countString = "select * from tblquiz where qstatus= $status AND userid='$_SESSION[numericuserid]' "; 
    } **// NOTE : THIS QUERY IS NOT WORKING** 

} 
+2

不是一個答案,但是現在有一個很大的機會,你有一個[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)易受攻擊的應用程序。 – PeeHaa

+1

qstatus缺少引號? – feathj

+1

99%的問題是由於沒有使用正確的[SQL佔位符](http://bobby-tables.com/php)來完成您的查詢構建而造成的。如果你使用這些技術,你不會有這樣的問題。你現在擁有它的方式,這些查詢完全運行將會是純粹的運氣。 – tadman

回答

0
select * from tblquiz where qstatus= '$status' 

QSTATUS缺失報價?

+1

即可SQL注入,再一次SQL注入 –

+1

。實際上,這整塊代碼都是一個集羣,但問題是「爲什麼它不起作用」,我回答了這個問題。顯然這個用戶應該刷新SQL安全。 – feathj

+0

僅僅因爲這個問題是具體的,並不意味着我們這些擁有更多知識的人不應該把它傳遞下去。 :-) –

0

在你的elseif子句中,檢查$ status是否爲空,如果是則設置$ querystring和$ countstring。這意味着您試圖查詢

qstatus= AND 

這應該會產生SQL錯誤。嘗試調試時打印出你的sql語句總是一個好主意。

+1

SQL注入和再次SQL注入 –

+1

@Adrian康沃爾好點,但問題不在於消毒SQL是嗎? –

+1

正確 - 但這正是索尼被Lulzsec黑客攻擊的原因。事實是,PHP是這樣的,應該讓OP意識到 - 問索尼是否認爲他們的系統是安全的。 OP是直接從HTTP請求中獲取不可信值 - 需要提及 –