MySQL错误1054当我尝试多个模型调用从一个控制器函数在CodeIgniter


MySQL error 1054 when I try more than one model call from a controller function in CodeIgniter

我想写一个函数,它将采取一系列字段和输入不同的值到不同的数据库。现在它只有两个单独的数据库条目,但我希望以后实现更多。我想在一个表中输入一个新的Saint,然后,如果用户填写'ofRegion'字段,我想将其存储在另一个表中。当模型试图输入"ofRegion"的信息时,我的问题就出现了。我得到一个MySQL错误(1054),指出有一个未知的列。我可以从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);
    }
}

非常感谢您的时间和工作!

这是因为您在插入数据之前使用$this作为数组来存储数据。$this是对整个对象的引用,您在其上设置的任何属性将持续存在,直到它们被取消设置。一种解决方案是将insert()函数改为数组,如下所示:

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);
}