PHP代码编辑器更新


PHP Update in codeigniter

我正在尝试编写一个函数来插入,更新和删除数据。我可以插入数据,但不能更新数据。问题是我不能传递id值到我的updateform。

控制器:

function update($id = 0){       
    $this->load->helper('form');  
    $this->load->helper('html');    
    $this->load->model('employees_model');  
    if($this->input->post('upsubmit')){             
        echo '<pre>';print_r($id);echo '</pre>';            
        exit(); 
        if($this->input->post('eid')){              
            $this->employees_model->entry_update();   
        }
    }                   
    $data = $this->employees_model->general();      
    if((int)$id > 0){               
        $query =this->employees_model->get($id);
        $data['txt_id']['value'] = $query['eid'];
        $data['txt_name']['value'] = $query['ename'];
        $data['txt_salary']['value'] = $query['esalary'];
        $data['txt_emailid']['value'] = $query['eemailid'];
    }       
    $this->load->view('employees_update',$data);        
}

模型:

function general(){
  $data['base']       = $this->config->item('base_url');
  $data['css']      = $this->config->item('css');
  $data['id']['value'] = 'eid';
  $data['name']     = 'txt_name';
  $data['salary']       = 'txt_salary';
  $data['emailId']  = 'txt_email';
  return $data; 
}   
function entry_update($id){
  $this->load->database();
  echo '<pre>';print_r($id);echo '</pre>';          
  exit();   
  $data = array(
          'ename'=>$this->input->post('txt_name'),
          'esalary'=>$this->input->post('txt_salary'),
          'eemailid'=>$this->input->post('txt_email'),            
        );  
  echo '<pre>';print_r($data);echo '</pre>';            
  exit();   
  $this->db->where('eid',$this->input->post($data));
  $this->db->update('employee',$data);  
  //echo '<pre>';print_r($data);echo '</pre>';          
  echo "Updated successfully";
}
function get($id)
{
 $this->load->database();
 $query = $this->db->get_where('employee',array('eid'=>$id));
 return $query->row_array();          
} 

请建议如何通过选择id值来更新视图部分的表单页面。

这是我的工作示例代码。希望能有所帮助。

控制器

   //I am using update from to preload data 
    function update_form($id)
    {
        $query=$this->product_model->get($id);
        $data['id']=$query['id'];
        $data['name']=$query['name'];
        $data['price']=$query['price'];
        $this->load->view('products/update_product',$data);
    }
function update()
{
    $this->product_model->save();
    redirect('products/index');
}

模型
    function get($id=0)
    {
        if($id==0)
        {
            $query=$this->db->get('product');
            return $query->result_array();
        }
        else
        {               
            $this->db->where('id',$id);
            $query=$this->db->get('product');
            return $query->row_array();
        }
    }
    // if **id** is greater than zero mean update, no **id** mean insert
    function save()
    {
        $id=$this->input->post('id');
        $name=$this->input->post('name');
        $price=$this->input->post('price');
        $data=array
            (
            'name'=>$name,
            'price'=>$price
            );
        if($id>0)
        {
            $this->db->where('id',$id);
            $this->db->update('product',$data);
        }
        else
        {
            $this->db->insert('product',$data);
        }
    }
}

视图(视图/产品/update_product.php)

<?php echo form_open('products/save');?> //form_open('controller_name/function_name');
    <?php echo form_hidden('id',$id);?>
    <?php echo form_input('name',$name,'placeholder="enter doctor name"');?><br>
    <?php echo form_input('price',$pricce,'placeholder="enter doctor phone number"');?><br>
    <?php echo form_submit('','Update');?>
<?php echo form_close();?>

id在更新中扮演重要的角色,你不应该把它作为隐藏字段包含在视图中

首先创建带有参数$id的函数。但是当你调用不带参数的函数时。修改这一行

in Controller change this line

$this->employees_model->entry_update();

 $this->employees_model->entry_update($this->input->post('eid'));

Model Change:

$this->db->where('eid',$this->input->post($data))

$this->db->where('eid',$this->input->post('eid'));

修改你的更新功能,如下所示:

function update($id = 0){       
    $this->load->helper('form');  
    $this->load->helper('html');    
    $this->load->model('employees_model');  
    if($this->input->post('upsubmit')){   
       $id= $this->input->post('eid');                    
        if($this->input->post('eid')){              
            $this->employees_model->entry_update();   
        }
    }                   
    $data = $this->employees_model->general();      
    if((int)$id > 0){               
        $query =this->employees_model->get($id);
        $data['txt_id']['value'] = $query['eid'];
        $data['txt_name']['value'] = $query['ename'];
        $data['txt_salary']['value'] = $query['esalary'];
        $data['txt_emailid']['value'] = $query['eemailid'];
    }       
    $this->load->view('employees_update',$data);        
}