记save函数

    在写商品修改的代码的时候,我使用到了create()函数,也使用到了save函数。但是我从表单接受过来的数据,在添加语句的时候都没成功。代码如下

 function upd($goods_id){
         $goods=D("Goods");
         if(!empty($_POST))
         {
             show($_POST);
             $goods->create();
             $result=$goods->where("goods_id=".$goods_id)->save();
             if($result){
                 $this->redirect("Goods/showlist");
             }
         }
         else{
          $goodsinfo=$goods->select();
         //show($goodsinfo);
          $this->assign("goodsinfo",$goodsinfo);     
          $this->display();
         }
 
    }

说出表单接受数据为

对比数据库

对比可以发现,表单的数据没主键。我查看手册save的用法

所以使用save的时候一定要有主键。后来我添加了where()。执行成功了,代码如下

function upd($goods_id){
         $goods=D("Goods");
         if(!empty($_POST))
         {
             show($_POST);
             $goods->create();
             $result=$goods->where("goods_id=".$goods_id)->save();
             if($result){
                 $this->redirect("Goods/showlist");
             }
         }
         else{
          $goodsinfo=$goods->where("goods_id=".$goods_id)->select();
         //show($goodsinfo);
          $this->assign("goodsinfo",$goodsinfo);     
          $this->display();
         }
 
    }

 

posted @ 2015-07-15 14:45  杰杰顺利  阅读(795)  评论(0编辑  收藏  举报