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.

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

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符