douzong5835 2013-04-19 08:23 采纳率: 100%
浏览 68
已采纳

如何使用连接表将记录添加到数据库 - codeigniter

I made a form for adding factories to my database. this works great but i can't show my factories on the page because they're in a joined table, but when i submit the page it does not add an id to the joined table so the factory won't be shown at the page.

the joined table looks like this:

bedrijfcategorieen
------------------
idbedrijfcat
idbedrijven
idcategorieen

Where idbedrijven is the id for my factories table.

My controller function for adding factories:

function bedrijven()
{
    $data['options'] = $ddmenu;
    $this->load->view('members/header');
    $this->load->view('members/editform', $data);
    $this->load->view('members/footer');
}

function addbedrijven()
{
    $this->members_model->addbedrijf();
    redirect('members/index');
}

my model function for adding factories:

function addbedrijf()
{
    $data = array(
       'idbedrijven' => $idbedrijven,
       'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
       'Postcode' => $this->input->post('Postcode'),
       'Plaats' => $this->input->post('Plaats'),
       'Telefoonnummer' => $this->input->post('Telefoonnummer'),
       'Email' => $this->input->post('Email'),
       'Website' => $this->input->post('Website'),
       'Profiel' => $this->input->post('Profiel'),
       'Adres' => $this->input->post('Adres'),
       'logo' => $this->input->post('logo')
    );

    $this->db->insert('bedrijven', $data);
}

i would like to add my factories trough the joined table. so it would be easier to add categories to the factories too.

I tried where('bedrijfcategorieen.idbedrijven = idbedrijven but it did not work.


table scheme

factories
---------
idfactories
factoryname
adress
postcode
country
telephone
...
...


categories
----------
idcategories
category

factorycategories
-----------------
idfactorycat
idfactories
idcategories

  • 写回答

1条回答 默认 最新

  • dsgwdigu84950 2013-04-22 05:45
    关注

    If I understand you right - you want to add records to two different tables via JOIN. This is not possible. You'll have two write separate insert statements for both the tables.

    Take a look at the Active Record's documentation http://ellislab.com/codeigniter/user-guide/database/helpers.html and read about $this->db->insert_id(); This may give you clues about how to write your insert statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?