foreach 数组插入到数据库

foreach array insert to database

我有 3 张桌子。类别、成分和 category_ingredient。现在我想将类别和成分的 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;
}

你的控制器

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 函数 saveIngredients($data) {

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


return $insert_id;

}

控制器代码

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

您将 $ingredient_id 视为字符串,但它是一个数组。我通常用我期望的类型命名我的变量,这样我就不会混淆......例如,$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) )
            );
    }

}