价格从高到低就正常,从低到高排序就不正确
price字段是decimal(20,2)类型<select name="picejia" id="picejia">
<option   selected="selected">--Default--</option>
<option value="1">high to low</option>
<option value="0">low to high</option>
</select>

<?php
$picejia = $_GET['picejia'];if($picejia==1 and !empty($picejia))
{
$sql="select * from products  where   order by price desc ";
}
elseif($picejia==0 and !empty($picejia))
{
$sql="select * from products where  order by price asc  ";
}
elseif(empty($picejia))
{
$sql="select * from products where  order by class_id desc";
}
?>

解决方案 »

  1.   

    后面加多 id desc例如:select * from products  where   order by price desc
    修改为:select * from products  where   order by price desc,id desc因为如果你的价格一样的话,排序就出问题了.加多一个标识就好了.
      

  2.   

     你这个判断有问题的. empty 0也表示false 
    if($picejia==1){
       $sql="select * from products  where   order by price desc ";
    }elseif($picejia==0){
       $sql="select * from products where  order by price asc  ";
    }else{
       $sql="select * from products where  order by class_id desc";
    }
    这样写就好了
      

  3.   

    我在机器上测试了OK。不管从小到大,还是从大到小!
    我的表结构如下:
    create table ceshi(
    id int(10) primary key AUTO_INCREMENT,               
    name varchar(20),           
    price decimal(20,2)
    )engine=myisam,charset=utf8;数据为:排序后由小到大。
         id  name  price   
          4  ceshi 9.16 
          2  ceshi 25.44 
          5  ceshi 29.99 
          1  ceshi 123.22 
          3  ceshi 244.25 
          6  ceshi 444.77 
          7  ceshi 9444.30 
      

  4.   

    执行前,看看sql是不是你想要的sql,如果是,拿着sql用mysql 客户端工具执行下,确认下是那里的问题
      

  5.   

    个人认为sql写错了,排序应该不需要加where的,应该是<select name="picejia" id="picejia">
    <option   selected="selected">--Default--</option>
    <option value="1">high to low</option>
    <option value="0">low to high</option>
    </select>
                        
    <?php                    
    $picejia = $_GET['picejia'];if($picejia==1)
    {
    $sql="select * from products  order by price desc ";//个人认为sql写错了,不需要where
    }
    elseif($picejia==0)
    {
    $sql="select * from products order by price asc  ";//个人认为sql写错了,不需要where
    }
    else
    {
    $sql="select * from products order by class_id desc";//个人认为sql写错了,不需要where
    }
    ?>通过了记得加分!