2011-08-23 70 views
0

从控制器的功能不止一个模型调用我想写这将采取一系列域和输入不同的值到不同的数据库的函数1054。目前它只有两个独立的数据库条目,但我希望稍后再实施。我想输入一个新的Saint到一个表中,然后,如果用户填入'ofRegion'字段,我希望将它存储在不同的表中。我的问题出现在模型尝试输入'ofRegion'的信息时。我得到一个MySQL错误(1054),说明有一个未知的列。我可以看到MySQL错误,它正在尝试输入以前条目的所有信息以及新信息。我如何清除旧信息?我可以做到这一点吗?或者我需要多个模型用于每个我想输入信息的表格?MySQL错误,当我尝试在笨

模型函数

public function input_saint($newSaintID) 
{ 
    //grab values from post stream 
    $this->saintID = $newSaintID; 
    $this->active = 1; 
    $this->nameFirst = $this->input->post('nameFirst'); 
    $this->nameLast = $this->input->post('nameLast'); 
    $this->gender = $this->input->post('gender'); 
    $this->martyr = $this->input->post('martyr'); 
    $this->nationality = $this->input->post('nationality'); 
    $this->feastMonth = $this->input->post('feastMonth'); 
    $this->feastDay = $this->input->post('feastDay'); 
    $this->about = $this->input->post('about'); 

    //insert information into the saint table 
    $this->db->insert('saint_table', $this); 
} 

public function set_of_region($newSaintID) 
{ 
    $this->saintID = $newSaintID; 
    $this->ofRegion = $this->input->post('ofRegion'); 
    $this->db->insert('saint_of_region', $this); 
} 

控制器功能

public function saint_input() 
{ 
    //Check if user is logged in, if they are not, send them to the login screen 
    if($this->session->userdata('logged_in') == FALSE) 
    { 
     redirect('base/'); 
    } 

    $this->load->library('form_validation'); 

    //load Saint model and get the nation list 
    $this->load->model('saint_model'); 

    //Load the nation list 
    $data['nationList'] = $this->saint_model->get_nations(); 

    if($this->form_validation->run('saint_input')==FALSE) 
    { 
     $this->load->view('std/top'); 
     $this->load->view('forms/saint_input', $data); 
     $this->load->view('std/bottom'); 
    } 
    else 
    { 
     //generate saintID 
     $newSaintID = $this->saint_model->get_largest_saintID(); 
     $newSaintID++; 

     $this->saint_model->input_saint($newSaintID); 

     //if specified, record the ofRegion 
     if($this->input->post('ofRegion') != NULL) 
     { 
      $this->saint_model->set_of_region($newSaintID); 
     } 

     //Send the user to this saint's single view page for review 
     redirect('base/display_one_saint/'.$newSaintID); 
    } 
} 

非常感谢您的时间和努力!

回答

1

那是因为你正在使用$此作为数组插入之前存储数据。 $这是对整个对象的引用,您设置的任何属性都将一直保留,直到它们未被设置。一种解决方案是改变为所述插入件(数组)用作以下:

public function set_of_region($newSaintID) 
{ 
    $ins_arr['saintID'] = $newSaintID; 
    $ins_arr['ofRegion'] = $this->input->post('ofRegion'); 
    $this->db->insert('saint_of_region', $ins_arr); 
}