解决方案 »

  1.   

    会不会是文章的sort并不是连续的,就前移来说你取sort-1作为调教来修改有可能没修改到数据库记录,然后即使当前sort+1可能还是没有比下个文章的sort大
      

  2.   


    /*前移*/
    if($result=='up'){
     $sql0=$db->query("select sort from ".$db->table('product')." where id='$id'");
     $row=$db->fetch_array($sql0);
     if(!$row){
      $oldpx=1;
     }else{
      $oldpx=$row['sort'];
     }
     if($oldpx>1){
     $db->query("update ".$db->table('product')." set sort=sort+1 where sort=".$oldpx-1);
     $db->query("update ".$db->table('product')." set sort=sort-1 where id=".$id);
     }
     header("Location:product_list.php\n");
     exit;
    }
    /*后移*/
    if($result=='down')
    {
      $sql1=$db->query("select sort from ".$db->table('product')." where id='$id'");
      $row=$db->fetch_array($sql1);
      if(!$row){
       $oldpx=9999;
      }else{
       $oldpx=$row['sort'];
      }
      $sql2=$db->query("select sort from ".$db->table('product')." order by sort desc limit 1");
      $rows=$db->fetch_array($sql2);
      if(!$rows){
       $maxpx=9999;
      }else{
       $maxpx=$rows['sort'];
      }
      if($oldpx<$maxpx){
       $db->query("update ".$db->table('product')." set sort=sort-1 where sort=".$oldpx+1);
       $db->query("update ".$db->table('product')." set sort=sort+1 where id=".$id);
      }
      header("Location:product_list.php\n");
      exit;
    }红色标注的那里改成 where id='".$id."'");
      

  3.   


    if($_REQUEST["action"] == "up"){
    uppx($_REQUEST['goods_id']);
    }
    elseif($_REQUEST["action"] == "down"){
    downpx($_REQUEST['goods_id']);
    }function uppx($goods_id)
    {
    if(!$goods_id)
    {
     $goods_id=0;
    }

    $sql=$GLOBALS['db']->query("select px from ".$GLOBALS['db']->table('product')." where id=".$goods_id);
    $rs=$GLOBALS['db']->fetch_array($sql);
    if(!$rs)
    {
     $oldpx = 1;
    }
    else
    {
     $oldpx = $rs['px'];
    }
        if($oldpx>1){
     $oldpx=$oldpx-1;
     $GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px+1 where px=".$oldpx);
     $GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px-1 where id=".$goods_id);
    }
        header("Location:product_list.php");
    exit;
    }function downpx($goods_id)
    {
    if(!$goods_id)
    {
     $goods_id=0;
    }
    $sql=$GLOBALS['db']->query("select px from ".$GLOBALS['db']->table('product')." where id=".$goods_id);
    $rs=$GLOBALS['db']->fetch_array($sql);
    if(!$rs)
    {
     $oldpx=1;
    }
    else
    {
     $oldpx=$rs['px'];
    }
    //假如$oldpx=1的话
        $sql1=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product')." order by px desc limit 1");
    $row=$GLOBALS['db']->fetch_array($sql1);
    if(!$row)
    {
     $maxpx=999;
    }
    else
    {
     $maxpx=$row['px'];
    }
    //到这里那么最大的$maxpx=4
        if($oldpx<$maxpx){
     $oldpx=$oldpx+1;
     $GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px-1 where px=".$oldpx);
     $GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px+1 where id=".$goods_id);
    }
    header("Location:product_list.php");
    exit;
    }根据你那代码我自己完整的演示了一遍,实现你要的效果了