【发布时间】:2016-01-21 03:06:43
【问题描述】:
我有 3 张桌子。类别,成分和类别_成分。现在我希望将类别和成分的 id 插入到 category_ingredient 中。但我收到这样的错误:
这是我的代码:
查看:add_ingredients.php
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select class="form-control" name="ingredient_category">
<option selected disabled>Select Ingredient Category</option>
<option value="All">All</option>
<?php foreach($this->products_model->getCategory() as $row): ?>
<option value="<?php echo $row->category_id ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)"></textarea>
</div>
</div>
<div class='form-group'>
<div class="col-sm-10">
<button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
</div>
</div>
<?php echo form_close(); ?>
控制器:dashboard.php
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
// var_dump($saveData); die();
$ingredient_id = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
$joinData[] = array(
'ingredient_id' => $ingredient_id,
'category_id' => trim($value)
);
}
//var_dump($joinData); die();
$this->products_model->saveCategoryIngredients($joinData);
redirect('dashboard/add_ingredients');
}/* end of upload_file() */
型号:products_model.php
public function saveIngredients($data)
{
foreach($data as $row => $value)
{
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function saveCategoryIngredients($data)
{
foreach($data as $row => $value)
{
$this->db->insert('category_ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
【问题讨论】:
-
您提交的“ingredient_category”是逗号分隔的吗?
-
哦,不,先生。因为我只想获取该字段的 id
标签: php html codeigniter foreach composite-primary-key