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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)