【发布时间】:2013-12-15 23:20:55
【问题描述】:
我的产品类别有多项选择,我有 3 个表格供他们选择。我的表格如下所示:
产品:
product_id | product_name
1 | my_package
类别:
category_id | category_name
1 | category 1
2 | category 2
3 | category 3
product_categories:
pc_id | product_id | category_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
当我想更新product_categories 表时,它不会更新。我只想为我的产品设置一个类别并删除另外两个类别,但是当我查看我的数据库时,我发现它更改了所有行,例如:
product_categories:
pc_id | product_id | category_id
1 | 1 | 2
2 | 1 | 2
3 | 1 | 2
这是我的代码:
foreach($selected_categories as $selected_category){
$selected_category_options[] = $selected_category->category_Id;
}
echo form_multiselect('product_category_id[]', $category_options, set_value('product_category_id', $selected_category_options));
这是我模型中的代码:
foreach($_POST['product_category_id'] as $key => $product_category_id){
$options = array(
'category_Id' => $product_category_id
);
$this->db->where('product_id', $options['product_id']);
$this->db->update('product_categories', $options);
}
【问题讨论】:
标签: php mysql codeigniter multiple-select