foreach数组插入数据库

时间:2016-01-21 03:06:43

标签: php html codeigniter foreach composite-primary-key

我有3张桌子。类别,成分和category_ingredient。现在我希望将类别和成分的id插入category_ingredient。但我收到这样的错误:enter image description here

这是我的代码:

查看: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(); ?>

CONTROLLER: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() */

MODEL: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;
}

enter image description here

3 个答案:

答案 0 :(得分:0)

您的控制器

public function uploadIngredients()
 {

   foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
    {
        $saveData = array('ingredient_id' => null,
                            'name'  => $value1
        );  
        $ingredient_id = $this->products_model->saveIngredients($saveData);
       $value1=$this->input->post('ingredient_category');
        $joinData = array(
                            'ingredient_id'     => $ingredient_id,
                            'category_id'       => trim($value)
        );

         $this->products_model->saveCategoryIngredients($joinData);
    }

        redirect('dashboard/add_ingredients');

    }

在您的模型上

    public function saveCategoryIngredients($data)
{

            $this->db->insert('category_ingredient', $value);
            $insert_id = $this->db->insert_id();  


    return $insert_id;
}

public function saveIngredients($ data)   {

        $this->db->insert('ingredient', $value);
        $insert_id = $this->db->insert_id();  


return $insert_id;

}

答案 1 :(得分:0)

控制器代码

public function uploadIngredients()
{
   $ingredients_post = explode(',', $this->input->post('ingredients'));    
    foreach( $ingredients_post as $key => $value)
    {
        $saveData = array('ingredient_id' => null,'name'  => trim($value)
        );  
        $ingredient_id = $this->products_model->saveIngredients($saveData);
        $joinData = array(
                            'ingredient_id'     => $ingredient_id,
                            'category_id'       => $this->input->post('ingredient_category')
                          );
        $this->products_model->saveCategoryIngredients($joinData);

    }
    redirect('dashboard/add_ingredients');
}

模型

 public function saveCategoryIngredients($data)
{

            $this->db->insert('category_ingredient', $value);

}
public function saveIngredients($data) {

        $this->db->insert('ingredient', $value);
        $insert_id = $this->db->insert_id();  


return $insert_id;
}

答案 2 :(得分:0)

您将$ingredient_id视为字符串,但它是一个数组。我通常使用类型I来命名我的变量,因此我不会感到困惑......例如,$ingredient_id_array

 // var_dump($saveData); die();
$ingredient_id_array = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
    foreach ( $ingredient_id_array as $key => $str ){
            $joinData[] = array(
                                'ingredient_id'     => intval( $str ),
                                'category_id'       => intval( trim($value) )
            );
    }

}