请教各位大牛一个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的回复。

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
