Question

I want to write a function which will take a series of fields and input different values into a different databases. Right now it is only two separate database entries but I hope to implement more later. I want to input a new Saint into one table and then, if the user fills in the 'ofRegion' field, I want that to be stored in a different table. My problem comes about when the model tries to input the information for 'ofRegion.' I get a MySQL error ( 1054 ) stating there is an unknown column. I can see by the MySQL error that it is trying to input all the information from the previous entry as well as the new information. How do I clear the old information? Can I even do this or will I need multiple models for each table I want to enter information into?

Model Functions

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

Controller Function

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

Thank you very much for your time and work!

Was it helpful?

Solution

That's because you're using $this as an array to store data before inserting it. $this is a reference to the object as a whole and any properties that you set on it will persist until they are unset. One solution is to change to an array for the insert() function as below:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top