2013-10-21 68 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 ''; 
    } 
}