使用codeigniter中的循环更新多行时出错

时间:2017-04-10 13:33:35

标签: php codeigniter

当我尝试使用for循环更新细节时,它会更新为空(Null)以引用列。

以下是我的代码,请帮助我解释我的逻辑错误。

查看代码
将生成的文本字段数取决于否。表中列出的值它正在foreach循环中执行

<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>" 
<?php
if ($advisor_emp->num_rows()>0 )
{
  $i=1;
  foreach($advisor_emp->result() as $data)
  {
    $eid = $data->id; 
    $cn = $data->company_name;
    $fd = $data->from_date;
    $td = $data->to_date;
    $tb = $data->type_of_buisness;
    $c1 = $data->company_addres_lane_1;
    $c2 = $data->company_addres_lane_2;
    $cc = $data->c_city;
    $cp = $data->c_pincode;
    $cs = $data->c_state;
    $cun = $data->c_country; ?>

    <div class="col-md-6">
         <div class="form-group">
             <input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
         </div>
         <div class="col-md-12">
              <div class="form-group">                  
                  <label>Previous Employer</label>
                  <input type="text" class="form-control" name="pcmp" value="<?php echo $cn; ?>">
               </div>
         </div>
         <div class="col-md-6">
             <div class="form-group">
                 <label>From</label>
                 <input type="text" name="pcmpfrom" class="form-control" value="<?php echo $fd ; ?>">
             </div>
         </div>
         <div class="col-md-6">
              <div class="form-group">
                   <label>To</label>
                   <input type="text" name="pcmpto" class="form-control" value="<?php echo $td ; ?>">
              </div>
         </div>
         <div class="col-md-12">
             <div class="form-group">
                <label>Designation</label>
                <input type="text" name="pcmpdesi" class="form-control" value="<?php echo  $tb;?>">
             </div>
         </div>
         <div class="col-md-6">
             <div class="form-group">
                 <label>Company Address Line 1</label>
                 <input type="text" name="pcmpadr1[]" class="form-control" value="<?php echo  $c1;?>">
             </div>
         </div>
         <div class="col-md-6">
             <div class="form-group">
                 <label>Company Address Line 2</label>
                 <input type="text" name="pcmpadr2" class="form-control" value="<?php echo  $c2;?>">
             </div>
         </div>
         <div class="col-md-6">
            <div class="form-group">
                <label>City</label>
                <input type="text" name="pcmpcit" class="form-control" value="<?php echo  $cc;?>">
            </div>
         </div>
         <div class="col-md-6">
             <div class="form-group">
                  <label>Pin Code</label>
                  <input type="text" name="pcmppin[]" class="form-control" value="<?php echo  $cp;?>">
              </div>
         </div>
         <div class="col-md-6">
             <div class="form-group">
                 <label>State</label>
                 <input type="text" name="pcmpstat[]" class="form-control" value="<?php echo $cs; ?>">
             </div>
          </div>
          <div class="col-md-6">
              <div class="form-group">
                  <label>Country</label>
                  <input type="text" name="pcmpcun[]" class="form-control" value="<?php echo $cun; ?>">
              </div>
          </div>
      </div>
      <?php 
        $i++;
      }  
  }
?>
<div class="col-md-12"
    <input type="submit" class="btn btn-primary" name="advisor_update" value="Update" >
</div>
</form>

这是我的控制器
在控制器中,我将使用for循环从视图中获取数据并将收到的数据发送到模态。

if ($this->input->post('advisor_update'))
{
    $emp_his_id = $this->input->post('eid');    
    $count_eid = count($emp_his_id);
       for($i=0;$i<$count_eid;$i++)
       {                        
            $emp_his_pk = $emp_his_id[$i];
            $inserte['company_name']= $this->input->post('cn');
            $inserte['from_date']= $this->input->post('fd');
            $inserte['to_date']= $this->input->post('td');
            $inserte['type_of_buisness']= $this->input->post('tb');
            $inserte['company_addres_lane_1']= $this->input->post('c1');
            $inserte['company_addres_lane_2']= $this->input->post('c2');
            $inserte['c_city']= $this->input->post('cc');
            $inserte['c_pincode']= $this->input->post('cp');
            $inserte['updated_on'] = date("Y-m-d");




$up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
        }
   }

这是我的模态

function update_employe($inserte=array(),$emp_his_pk)
{
    $this->db->where('id',$emp_his_pk);
    $result=$this->db->update("is_employment", $inserte);
    return $result;
}

1 个答案:

答案 0 :(得分:0)

查看代码

我已更改您的视图代码输入名称

<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>" >
<?php
if ($advisor_emp->num_rows()>0 )
{
  foreach($advisor_emp->result() as $data)
  {
    $eid = $data->id; 
    $cn = $data->company_name;
    $fd = $data->from_date;
    $td = $data->to_date;
    $tb = $data->type_of_buisness;
    $c1 = $data->company_addres_lane_1;
    $c2 = $data->company_addres_lane_2;
    $cc = $data->c_city;
    $cp = $data->c_pincode;
    $cs = $data->c_state;
    $cun = $data->c_country; ?>

<div class="col-md-6">
     <div class="form-group">
         <input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
     </div>
     <div class="col-md-12">
          <div class="form-group">                  
              <label>Previous Employer</label>
              <input type="text" class="form-control" name="cn[]" value="<?php echo $cn; ?>">
           </div>
     </div>
     <div class="col-md-6">
         <div class="form-group">
             <label>From</label>
             <input type="text" name="fd[]" class="form-control" value="<?php echo $fd ; ?>">
         </div>
     </div>
     <div class="col-md-6">
          <div class="form-group">
               <label>To</label>
               <input type="text" name="td[]" class="form-control" value="<?php echo $td ; ?>">
          </div>
     </div>
     <div class="col-md-12">
         <div class="form-group">
            <label>Designation</label>
            <input type="text" name="tb[]" class="form-control" value="<?php echo  $tb;?>">
         </div>
     </div>
     <div class="col-md-6">
         <div class="form-group">
             <label>Company Address Line 1</label>
             <input type="text" name="c1[]" class="form-control" value="<?php echo  $c1;?>">
         </div>
     </div>
     <div class="col-md-6">
         <div class="form-group">
             <label>Company Address Line 2</label>
             <input type="text" name="c2[]" class="form-control" value="<?php echo  $c2;?>">
         </div>
     </div>
     <div class="col-md-6">
        <div class="form-group">
            <label>City</label>
            <input type="text" name="cc[]" class="form-control" value="<?php echo  $cc;?>">
        </div>
     </div>
     <div class="col-md-6">
         <div class="form-group">
              <label>Pin Code</label>
              <input type="text" name="cp[]" class="form-control" value="<?php echo  $cp;?>">
          </div>
     </div>
     <div class="col-md-6">
         <div class="form-group">
             <label>State</label>
             <input type="text" name="cs[]" class="form-control" value="<?php echo $cs; ?>">
         </div>
      </div>
      <div class="col-md-6">
          <div class="form-group">
              <label>Country</label>
              <input type="text" name="cun[]" class="form-control" value="<?php echo $cun; ?>">
          </div>
      </div>
  </div>
  <?php 
      }  
  }
?>

控制器代码

你必须在控制器中运行一个foreach循环。

if ($this->input->post('advisor_update'))
{
       $emp_his_id = $this->input->post('eid');    
       $cn = $this->input->post('cn');
       $fd = $this->input->post('fd');
       $td = $this->input->post('td');
       $td = $this->input->post('tb');
       $c1 = $this->input->post('c1');
       $c2 = $this->input->post('c2');
       $cc = $this->input->post('cc');
       $cp = $this->input->post('cp');
       foreach($emp_his_id as $key => $eid)
       {                        
            $emp_his_pk = $eid;
            $inserte = array();
            $inserte['company_name']= $cn[$key];
            $inserte['from_date']= $fd[$key];
            $inserte['to_date']= $td[$key];
            $inserte['type_of_buisness']= $tb[$key];
            $inserte['company_addres_lane_1']= $c1[$key];
            $inserte['company_addres_lane_2']= $c2[$key];
            $inserte['c_city']= $cc[$key];
            $inserte['c_pincode']= $cp[$key];
            $inserte['updated_on'] = date("Y-m-d");

            $up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
        }
   }

希望这对你有用