Home > Backend Development > PHP Tutorial > THINKPHP add问题

THINKPHP add问题

WBOY
Release: 2016-06-23 13:29:10
Original
1125 people have browsed it

请教各位大牛一个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();  	      }    }	
Copy after login


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


回复讨论(解决方案)

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

怎么一个get一个post

提交的方式是啥?
POST?

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

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

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

@xuzuning 请问如何处理呢?

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template