-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 在查询中不起作用

4

4 回答 4

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!";
}
于 2013-10-21T14:29:13.827 回答
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);
于 2013-10-21T14:29:32.143 回答
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 不存在。这将证明这一点。

于 2013-10-21T14:31:29.970 回答
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 '';
    }
}
于 2013-10-21T14:32:13.487 回答