>백엔드 개발 >PHP 튜토리얼 >THINKPHP add问题

THINKPHP add问题

WBOY
WBOY원래의
2016-06-23 13:29:101118검색

请教各位大牛一个thinkphp问题:
问题描述如下:
我需要从页面上传递参数id过来,并且将id值写入到数据库中,但过程中发现,在submit的时候该参数被置为null值导致insert 不仅数据库。
其中$viewpoint_id得到的值为空
代码如下:

public function food_add() {        $Food=M('Food');           $FoodType=M("FoodType");        if (isset($_POST['save'])) {        	 //判断是否已经添加了菜品        	 $viewpoint_id= $_GET['id'];                 $id=$_POST['names'];		   $this->food_add2($id,$viewpoint_id);        	          } else {		    	  // 显示数据        	  $lists = $Food->field("food.name,food.food_type,food.price,food.id,manager.path")		                ->table($Food->getTableName()." food")		                ->join(M("file_manager")->getTableName()." manager on food.pic = manager.id")		                ->page($page)->order('food.id desc')->select();		        		        foreach ($lists as $k => $v) { 		            $lists[$k]['path'] = __ROOT__.$lists[$k]['path'];		            $lists[$k]['type_name'] = $FoodType->where("id={$v['food_type']}")->getField('type_name');		        }            $this->assign('lists', $lists);            $this->display('food_add');  		    }    }public function food_add2($id,$vid){    	    $Food=M('Food');             $FoodType=M("FoodType");          $FoodDinner=M('ViewpointDinner');      	  //获取FOODID      	  $food_id= implode(',' ,$_POST['names']);      	  $food_detail=$Food->where(array('id'=>array('in',$food_id)))->select();      	  $FoodDinner->create();          foreach ($food_detail as $k => $v) {		    	  	$FoodDinner->id = $food_detail[$k]['id'];		          $FoodDinner->type = $food_detail[$k]['food_type'];		          $FoodDinner->names = $food_detail[$k]['name'];		          $FoodDinner->price = $food_detail[$k]['price'];		          $FoodDinner->pic = $food_detail[$k]['pic'];		          $FoodDinner->create_time = strtotime(date('Y-m-d H:i:s'));		          $FoodDinner->sort=1; //排序		          $FoodDinner->is_low=1; //低消		          $FoodDinner->viewpoint_id=$vid;		          $FoodDinner->add();  	      }    }	


尝试过在外层获取$viewpoint_id,可以获取值,但到了foreache时,就为空了,请问这个问题怎么处理呢?


回复讨论(解决方案)

//判断是否已经添加了菜品             $viewpoint_id= $_GET['id'];                 $id=$_POST['names'];

怎么一个get一个post

提交的方式是啥?
POST?

@namelesswei @shengli881026 感谢两位回复,提交是POST提交,$_GET['id']是获取从其他页面传过来的参数
传参链接如下:

 <a href="__URL__/food_add/id/{$vo.id}">添加低消</a> 

你的 $_POST['save'] 和 $_GET['id'] 分属于两个不同的来源
所以这两个值不可能同时存在

@xuzuning 请问如何处理呢?

表单有 action 属性,你的 id 可写在它上面
一个在表单里应隐藏控件传递,不过就不是 $_GET['id'] 而是 $_POST['id'] 了

@xuzuning  谢谢你的回复,已经解决了我的问题。也感谢@namelesswei @shengli881026的回复。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:LNMP环境搭建-PHP篇다음 기사:PHP开发环境的搭建