0

我必须制作一个汽车网站,根据预设的 mysql 数据库过滤汽车。如果全部被选中(例如:本田,白色,汽油而不是特别 - 它会显示那辆车),我会过滤它,但如果我只想查看所有本田的(例如)什么都没有显示。

这是我的代码:

if(isset($_GET['make']) || isset($_GET['colour']) || isset($_GET['fueltype']) ||          isset($_GET['special'])){

if(isset($_GET['make'])){
$make = $_GET['make'];
}

if(isset($_GET['colour'])){
$colour = $_GET['colour'];
}

if(isset($_GET['fueltype'])){
$fueltype = $_GET['fueltype'];
}

if(isset($_GET['special'])){
$special = $_GET['special'];
}

$result = mysqli_query($con,"SELECT * FROM Cars WHERE MAKE ='$make' AND COLOUR = '$colour' AND FUELTYPE = '$fueltype' AND SPECIAL = '$special'");
}



else{
$result = mysqli_query($con,"SELECT * FROM Cars");
}

else 语句使您在过滤之前打开页面时显示所有汽车。

4

4 回答 4

0

这是你如何做到的。请为sql注入攻击做点什么。这只是一个例子。

$query  =   "SELECT * FROM Cars ";
$where  =   array();

if(isset($_GET['make'])){
    $where['make'] = $_GET['make'];
}

if(isset($_GET['colour'])){
    $where['colour'] = $_GET['colour'];
}

if(isset($_GET['fueltype'])){
    $where['fueltype'] = $_GET['fueltype'];
}

if(isset($_GET['special'])){
    $where['special'] = $_GET['special'];
}

$string =   '';
if(count($where)>0){
    $i=0;
    foreach($where as $key => $value)
    {
        if($i==0){
            $string .=  " WHERE $key = $value ";
        }else{
            $string .=  " AND $key = $value ";
        }
    $i++;   
    }
}
$query  .=  $string;

mysqli_query($query);
于 2013-05-08T08:03:48.243 回答
0
if(isset($_GET['make']) || isset($_GET['colour']) || isset($_GET['fueltype']) ||          isset($_GET['special'])){
$where="WHERE ";
if(isset($_GET['make'])){
$make = $_GET['make'];
$where .="MAKE ='$make' AND";
}

if(isset($_GET['colour'])){
$colour = $_GET['colour'];
$where .="COLOUR = '$colour' AND";
}

if(isset($_GET['fueltype'])){
$fueltype = $_GET['fueltype'];
$where .="FUELTYPE = '$fueltype' AND";
}

if(isset($_GET['special'])){
$special = $_GET['special'];
$where .="SPECIAL = '$special";
}
$where=rtrim($where,'AND');

$result = mysqli_query($con,"SELECT * FROM Cars".$where);
}



else{
$result = mysqli_query($con,"SELECT * FROM Cars");
}
于 2013-05-08T08:04:00.013 回答
0

转义可能意味着:mysql_escape_string,检查有效/允许值列表,或者经过深思熟虑后想要的任何内容。

$where = "";
$separator = " WHERE ";

if(isset($_GET['make'])){
    $make = $_GET['make'];
    !! escape here
    $where .= $separator . " MAKE = '$make' ";
    $separator = " AND ";
}

if(isset($_GET['colour'])){
    $colour = $_GET['colour'];
    !! escape here
    $where .= $separator . " COLOUR = '$colour' ";
    $separator = " AND ";
}

... 

$result = mysqli_query($con,"SELECT * FROM Cars " . $where);
于 2013-05-08T08:01:27.473 回答
0

忽略安全问题,您可以将整个代码替换为以下内容:

$query = "SELECT * FROM Cars WHERE 1 = 1";

if (isset($_GET['make']))
    $query .= ' AND MAKE = ' . (int) $_GET['make'];

if (isset($_GET['colour']))
    $query .= ' AND COLOUR = ' . (int) $_GET['colour'];

if (isset($_GET['fueltype']))
    $query .= ' AND FUELTYPE = ' . (int) $_GET['fueltype'];

if (isset($_GET['special']))
    $query .= ' AND SPECIAL = ' . (int) $_GET['special'];

$result = mysqli_query($con, $query);
于 2013-05-08T08:07:07.927 回答