【问题标题】:foreach array insert to databaseforeach 数组插入数据库
【发布时间】: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


【解决方案1】:

您将$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) )
            );
    }

}

【讨论】:

  • 先生,我试过你的代码,当我 var_dump($joinData);死();它就像这个数组 (size=2) 0 => array (size=2) 'ingredient_id' => int 9 'category_id' => string '2' (length=1) 1 => array (size=2) 'ingredient_id ' => int 10 'category_id' => 字符串 '2' (length=1)
  • category_id 仍然是一个字符串 :((((
  • 先生,它仍然是 category_id 中的一个字符串
  • 是的,好吧,然后将其转换为数字 -> intval( )... 看起来您应该检查您的数据类型并更加注意命名变量。您的短代码很好,但不清楚您是否将其传递给下一个人
  • 它现在是一个数字,但它没有保存在我的桌子上 category_ingredient 先生 :(
【解决方案2】:

你的控制器

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

公共函数 saveIngredients($data) {

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


return $insert_id;

}

【讨论】:

  • 数据是保存成分,但我无法将 2 个表成分和类别的 ID 插入到 category_ingredient :( 它不起作用先生 :(
  • 还有你给先生的代码只插入1个数据。我试图在我的表中插入多个数据先生
  • 例如,在我的文本区域中,我想添加洋葱、苹果、大蒜等成分,因此在我的数据库表成分中,它被分解并且 id 不一样。
  • 它只插入一种成分而不是多种成分先生
  • 尝试获得你爆炸的价值,例如 $abc=explode(',', $this->input->post('ingredients'); print_r($abc);
【解决方案3】:

控制器代码

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

【讨论】:

  • maam 我试过你的代码,但它只插入了 1 个值 :(
  • 我已经修改了代码,请您再检查一下,如果它不起作用,请告诉我 $ingredients_post 的值
猜你喜欢
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
相关资源
最近更新 更多