同功能编辑和插入在Codeigniter

问题描述:

OKay所以我正在开发电子商务网站,在那里我使用Common View文件来编辑和插入数据。问题是我已经为添加和插入做了一个共同的功能。所以,如果我们编辑表单,它的ID就是通过URL传递的。该函数获取URL并操作数据并查看编辑后的表单。问题是插入数据。我的通用函数接受一个参数[编辑时正在传递的ID]。当我尝试加载“添加文件”时,它给我错误。同功能编辑和插入在Codeigniter

Missing argument 1 for Register::product() 

这里是我的代码:

Controller: 
function product($get_product_id) 
    { 
     //DO Something 
     //Check POST data 
     // add OR UPDATE POST Data 
    } 

View File: 
<? if(isset($data)) 
     { 
      foreach ($data as $data1) 
      { 
      $get_product_id=$data1->id; 
      $get_product_name=$data1->product_name; 
      $get_product_code=$data1->code; 
      $get_product_price=$data1->price; 
      $get_product_quantity=$data1->quantity; 
      $get_product_status=$data1->status; 
      $get_product_details=$data1->details; 
      $get_product_image=$data1->productimage; 

      } 

     } 
     else 
     { 
      $get_product_id=""; 
      $get_product_name=""; 
      $get_product_code=""; 
      $get_product_price=""; 
      $get_product_quantity=""; 
      $get_product_status=""; 
      $get_product_details=""; 
      $get_product_image=""; 
     } 
     ?> 
<? echo form_open_multipart('register/product/'.$get_product_id.'');?> 
<table width="90%" border="0" cellspacing="0" cellpadding="6"> 

<tr> 
    <td width="20%" align="right"><? echo form_label('Product Name');?></td> 
    <td width="80%"><?echo form_input(array('id' =>'product_name','name' =>'product_name','size' => '64','value' => $get_product_name));echo form_hidden('id', $get_product_id);?> 
     <br/> 
    </td> 
</tr> 
//And So on... 
+1

用户功能产物($ get_product_id = NULL){ // 做某事 //检查POST数据 //添加或更新数据POST} –

控制器代码应该是

Controller: 
    function product($get_product_id = NULL) { 
    if ($get_product_id) { 
     //edit code 
    } else { 
     //insert code 
    } 

    //DO Something 
    //Check POST data 
    // add OR UPDATE POST Data 
} 

Controller: 
function product($get_product_id==NULL) // This means that you are assigning the default value if the function gets a null value for the argument $get_product_id 
    { 
     //DO Something 
     //Check POST data 
     // add OR UPDATE POST Data 
    } 

请更改这样的说法,做任何你want.Inside功能您必须检查$get_product_id是否为空,如果$get_product_id i s null您可以决定请求是否插入,否则请求是更新请求