在sql中一次更新两个表中的相同id

时间:2014-06-23 08:11:04

标签: mysql codeigniter

如果我更新它,如何在两个不同的表中一次更新一个id和名称,只更改另一个表中没有更新的一个表中的id。我复制了控制器,并查看。 这是总代码可以任何人帮我如何在两个表中一次更新。 型号:

function updatecategory($cat_sub_cat_id)    
{

    $this->db->trans_start();
    $data=array(
        'cat_id'=>$this->input->post('cat_id'),
        'name'=>$this->input->post('mainc')
    );

    $this->db->where('cat_id',$cat_sub_cat_id); 
    $this->db->update('category',$data);

    $data=array(
        'cat_id'=>$this->input->post('cat_id')
    );

    $this->db->where('cat_sub_cat_id',$cat_sub_cat_id); 
    $this->db->update('category_subcategory_association',$data);

    $this->db->trans_complete();

}

控制器:

function editcategory()
{
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
    $this->form_validation->set_rules('cat_id','Category Id','required');
    $this->form_validation->set_rules('name','Name','required');
    if($this->form_validation->run()==FALSE)
    {

    $data['records']= $this->bulkupload_model->get_category($this->input->post('cat_sub_cat_id'));
    $data['mode'] = "edit";
    $data['mainpage'] = "category";
    $this->load->view('includes/mainpage', $data);
      }
else
{
    $this->bulkupload_model ->updatecategory($this->input->post('cat_sub_cat_id'));         
    if(is_array($result)):      
    $data['records']=$this->bulkupload_model->get_category($this->uri->segment('cat_sub_cat_id'));      
    $data['errors']=$result;                           
    $data['mainpage']='category';                               
    $data['mode']='edit';                          
    $this->load->view('includes/mainpage',$data);
    else:
    $this->flash->success('Successfully Updated the record.');
    redirect('uploads/index');
    endif;

     }
}

查看:

<form id="validation" class="form-horizontal" action="<?php echo base_url();?>uploads/editcategory" method="POST">            
                        <?php echo form_hidden('cat_sub_cat_id',$record->cat_sub_cat_id);?>
                        <div class="row-form clearfix">
                            <div class="span3"><label for="cat_id">Category Id</label></div>
                            <div class="span9"><input class="validate[required]" type="text" id="cat_id" name="cat_id" value="<?php echo $record->cat_id;?>"/></div>

                        </div>   
                        <div class="row-form clearfix">
                            <div class="span3"> <label for="name">Category Name</label>   </div>
                            <div class="span9"><input class="validate[required]" type="text" id="name" name="name" value="<?php echo $record->mainc;?>"/></div>
                        </div>   

1 个答案:

答案 0 :(得分:0)

可能是你在这里打错了吗?

$this->bulkupload_model ->updatecategory($this->input->post('cat_sub_cat_id'));
//---------------------^

应该看起来像

$this->bulkupload_model->updatecategory($this->input->post('cat_sub_cat_id'));