2013-10-21 70 views
-3

有人能告訴我什麼是此錯誤:變量選擇查詢

if (isset($_GET['preis']) AND $_GET['preis']==="0-100-euro"){ 
    $preis = "WHERE preis >= 0 and preis <= 100"; 
} 
if (isset($_GET['preis']) AND $_GET['preis']==="100-200-euro"){ 
    $preis = "WHERE preis >= 100 and preis <= 200"; 
} 
if (isset($_GET['preis']) AND $_GET['preis']==="200-300-euro"){ 
    $preis = "WHERE preis >= 200 and preis <= 300"; 
} 
?> 


$abfrage = "SELECT * FROM outfits $preis LIMIT $start, $eintraege_pro_seite"; 
$ergebnis = mysql_query($abfrage); 

$ PREIS沒有在查詢工作

回答

0

請嘗試以下代碼

<?php 
if (isset($_GET['preis']) && $_GET['preis']==="0-100-euro") 
{ 
    $preis = "WHERE preis >= 0 and preis <= 100"; 
} 
elseif (isset($_GET['preis']) && $_GET['preis']==="100-200-euro") 
{ 
    $preis = "WHERE preis >= 100 and preis <= 200"; 
} 
elseif (isset($_GET['preis']) && $_GET['preis']==="200-300-euro") 
{ 
    $preis = "WHERE preis >= 200 and preis <= 300"; 
} 
else 
{ 
    $preis = "WHERE preis >= 200 and preis <= 300"; 
} 


$abfrage = "SELECT * FROM outfits $preis LIMIT $start, $eintraege_pro_seite"; 
$ergebnis = mysql_query($abfrage); 
?> 

總是試圖使用elseif或一個案例。如果以前的規則不匹配,則在末尾使用else意味着將會有WHERE語句。它可能僅僅是這樣簡單的事情,條件不符合,因此$ abfrage不存在。這將證明這一點。

+0

但你應該使用一個開關而不是一個ifelse叢林。 – Popnoodles

+0

elseif和&&而不是AND正在工作。 Bt沒有重寫規則:RewriteRule^preis - ([^ - ] *)/ $?preis = $ 1 [L]任何想法? – Bunghole

+0

@CaroFiedler RewriteRule什麼時候開始發揮作用?你應該在你的問題中提到這一點。另外我知道我對'&&'邏輯運算符是正確的;-) –

0

這是很難說,如果你不給我們的一個例子值爲$_GET['preis']

在你的情況我想沒有if的被評估爲真,也這可能是更容易:

$preis=""; 
if(isset($_GET['preis'])){ 
    $g_preis=$_GET['preis']; 
    $parts=explode('-', $g_preis); 
    $preis="WHERE preis >= $parts[0] and preis <= $parts[1]"; 
}else{ 
    echo "NO query for you!"; 
} 
+0

爲什麼downvote呢? – Popnoodles

+0

Yikes! '$ _GET ['preis']'直接在查詢中! –

+0

只是改變了OP在做什麼。應檢查內容,或使用mysqli_函數。 – Naryl

0
if (isset($_GET['preis']) AND $_GET['preis']==="0-100-euro"){ 
$preis = "WHERE preis >= 0 and preis <= 100"; 
} 
if (isset($_GET['preis']) AND $_GET['preis']==="100-200-euro"){ 
$preis = "WHERE preis >= 100 and preis <= 200"; 
} 
if (isset($_GET['preis']) AND $_GET['preis']==="200-300-euro"){ 
$preis = "WHERE preis >= 200 and preis <= 300"; 
} 
?> 


$abfrage = "SELECT * FROM outfits ".$preis." LIMIT $start, $eintraege_pro_seite"; 
$ergebnis = mysql_query($abfrage); 
+0

這不會有什麼區別 – Popnoodles

+0

@popnoodles同意了,看不出這個好處。 –

0

請確保您轉義所有字符串值來應對SQL注入攻擊。此外,如果叢林將使你的代碼相當難以維護。

看一看這個

$sPresis = (isset($_GET['preis'])) ? $_GET['preis'] : ''; 

$abfrage = 'SELECT * FROM outfits WHERE 1=1 ' . $this->buildWhereQuery($sPresis) .'LIMIT ' . implode(',', array($start, $eintraege_pro_seite)); 
$ergebnis = mysql_query($abfrage); 


function buildWhereQuery($sPresis){ 
    $sPresis = (string) $sPresis; // mysqli escape this as well 

    switch($sPresis){ 
     case "0-100-euro": 
      return 'AND preis >= 0 AND preis <= 100'; 

     case "100-200-euro": 
      return 'AND preis >= 100 AND preis <= 200'; 

     case "200-300-euro": 
      return 'AND preis >= 200 AND preis <= 300'; 
     default: 
      return ''; 
    } 
}