Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未定义的变量混淆代码点火器活动记录_Php_Arrays_Database_Codeigniter - Fatal编程技术网

Php 未定义的变量混淆代码点火器活动记录

Php 未定义的变量混淆代码点火器活动记录,php,arrays,database,codeigniter,Php,Arrays,Database,Codeigniter,我有以下代码 这是我处理textfield输入类型的地方,我在其中输入配方的成分。 输入成分的正确格式如下所示 5毫升牛奶,4毫升水,2个胡萝卜 由于数量、计量单位和名称的列不同,所以两者之间总是有分隔符 我对分离没有问题。我还有一张桌子。这就是所谓的配料。 这就是我填充不同成分的地方,比如牛奶,并把它们放在它们所属的食物组中,以及它们的营养信息(它会释放出什么,比如钙) 无论如何,我得到了一个数据库错误,不知何故,当我在所有的处理之后尝试插入时,类型和属性都是空的。我想了想,也许我的问题错了?

我有以下代码

这是我处理textfield输入类型的地方,我在其中输入配方的成分。 输入成分的正确格式如下所示

5毫升牛奶,4毫升水,2个胡萝卜

由于数量、计量单位和名称的列不同,所以两者之间总是有分隔符

我对分离没有问题。我还有一张桌子。这就是所谓的配料。 这就是我填充不同成分的地方,比如牛奶,并把它们放在它们所属的食物组中,以及它们的营养信息(它会释放出什么,比如钙)

无论如何,我得到了一个数据库错误,不知何故,当我在所有的处理之后尝试插入时,类型和属性都是空的。我想了想,也许我的问题错了?但它没有发出任何警告或通知。虽然它只给了我一个通知/警告,当我试图在数组$comp中声明它们时,这两个值都是未定义的,而数组$comp是要插入的值

我得到这个错误

错误号码:1048

“营养”列不能为空

插入
组件
组件名称
数量
单位
营养
类型
)值('milk','5','ml',NULL,NULL)

文件名:C:\www\KG\system\database\DB\u driver.php

电话号码:330

守则:

function insertrecipe()
    {
        $ingredients = $this->input->post('ingredients');
        $recipename = $this->input->post('recipename');

        $recipe = array(
            'recipename' => $recipename,
            'description' => $this->input->post('description'),
            'instruction' => $this->input->post('instructions'),
            'serving' => $this->input->post('serving'),
            );      

        $this->db->insert('recipe', $recipe);

        $this->ingredient($ingredients,$recipename);
    }

    function ingredient($ingredients,$recipename)
    {
    $ids = array();
    $first_slice = explode(',',$ingredients);
    $secondslice = $this->second_slice($first_slice);


            foreach($secondslice as $qty => $iname)
            {
                $arr = explode('-',$qty);
                $third_slice[$arr[1]] = $arr[0];

                $sql = $this->db
                            ->select('nutrition,group')
                            ->where('ingname', $iname)
                            ->from('ingredients')
                            ->get()
                            ->result_array();

                $result = $this->db->query($sql);

                foreach($result->result_array() as $row)
                {
                $ingredient_type = $this->get_ingredient_type($row['group']);
                }

                foreach($third_slice as $measure => $num)
                {
                $comp = array(
                                'componentname' => $iname,
                                'quantity' => $num,
                                'unit' => $measure,
                                'nutrition' => $nutri,
                                'type' => $ingredient_type
                                );
                    if($insert2 = $this->db->insert('component',$comp))
                    {   
                        $latest_id = $this->db->insert_id();

                        $ids[] = $latest_id;
                    }
                }

            }
    }



    function second_slice($data)
    {
            foreach($data as $key => $value)
            {
            $ingredient = explode(' ', trim($value));
            $second_slice[$ingredient[0]] = $ingredient[1];
            }

    return $second_slice;
    }


    function get_ingredient_type($data)
    {
                //foreach($data->result_array() as $row)
                //{ 
                    if($data['group'] == "vegetable")
                    {
                        $type = 1;
                    }
                    else if($data['group'] == "fruit")
                    {
                        $type = 2;
                    }
                    else if($data['group'] == "dairy")
                    {
                        $type = 3;
                    }
                    else if($data['group'] == "seafood")
                    {
                        $type = 4;
                    }
                    else
                    {
                        $type = 5;
                    }
                //}
    return $type;
    }
组件的数据库表包含以下列

componentid componentname   quantity    unit    nutrition   type
营养是varchar,单位是int。我猜它们不是null或不接受null值的col


我已经将不同的foreach循环分离为函数。我最初认为错误是因为我在该函数中的每个循环都有3-5个循环。所以我决定把它们分成函数。

我一直在编辑我的代码。我也将感谢编码建议。例如,如果有更好的方法来完成我所做的事情。打印数据库表。另外,问题是当我插入的两个值都为空时。这不是真正的桌子。我想,不知怎么的,我很困惑。不管怎样,我编辑过。