如何使用codeigniter

时间:2018-12-26 05:05:52

标签: php mysql codeigniter-3

我为存储在数据库中的多个数据插入创建了一个动态表格,这里我们有两个表'kly_shops',' kly_flex_details '。' kly_shops ”存储的商店详细信息。 kly_flex_details ”存储的弹性详细信息。它是一种动态形式,用于存储商店中的不同弹性详细信息。下面给出的我正在使用方法

 $fdate=date('Y-m-d', strtotime(strtr($this->input->post('FDate'), '/', '-')));
    $tdate=date('Y-m-d', strtotime(strtr($this->input->post('TDate'), '/', '-')));
   $data=array('shop_name'=>$this->input->post('shopname'),
                 'location'=>$this->input->post('location'),
                'from_date'=>$fdate,
                'to_date'=> $tdate,
                'printing_vendor_id'=>$this->input->post('printvendor'),
                'fixing_vendor_id'=>$this->input->post('fixvendor'),
                'total_amount'=>$this->input->post('Amount'),
                'flex_count'=>$this->input->post('flexcount'));
   $this->Shop_Model->add_shops($data); 
  $insert_id= $this->db->insert_id();

    $newfdate=date('Y-m-d', strtotime(strtr($this->input->post('bindfdate'), '/', '-')));
    $newtdate=date('Y-m-d', strtotime(strtr($this->input->post('bindtdate'), '/', '-')));

    $data= array(
        'shop_id'=>$insert_id,
        'type'=>$this->input->post('type'),
        'size'=>$this->input->post('size') ,
        'name'=>$this->input->post('name'),
        'from_date'=>$newfdate,
        'to_date'=>$newtdate);
        $this->Shop_Model->add_flexes($data);
        $count=count($this->input->post('atype'));
        $type=$this->input->post('atype');
            foreach( $type as $i=>$a)
            // for($i=0; $i<$count; $i++)
            {

                $frdate=date('Y-m-d', strtotime(strtr($this->input->post('abindfdate')[$i], '/', '-')));
                $trddate= date('Y-m-d',strtotime(strtr($this->input->post('abindtdate')[$i], '/', '-')));
                $data = array(
                    'shop_id'=>$insert_id,
                    'type'=>$a,
                    'size'=>$this->input->post('asize')[$i] ,
                    'name'=>$this->input->post('aname')[$i],
                    'from_date'=> $frdate,
                    'to_date'=>$trddate
                    );

                $this->Shop_Model->add_flexes($data);

            }

插入方法正确完成,但出现数据库错误,即

Error Number: 1048

Column 'shop_name' cannot be null
INSERT INTO `kly_shops` (`shop_name`, `location`, `from_date`, `to_date`, `printing_vendor_id`, `fixing_vendor_id`, `total_amount`, `flex_count`) VALUES (NULL, NULL, '1970-01-01', '1970-01-01', NULL, NULL, NULL, NULL)

Filename: D:/xampp/htdocs/branding/klth_system/database/DB_driver.php

Line Number: 691

3 个答案:

答案 0 :(得分:0)

function Act_AddProducts() 
{
   $prodnames = $this->input->post( 'prodname' );
   $prodrates = $this->input->post( 'rate' );
    if ( ! empty($prodnames) && ! empty($prodrates) ) 
    {
        foreach ($prodnames as $key => $value ) 
        {
            $data['product_name'] = $value;
            /* make sure product_rate columns is correct i just guess it*/
            $data['product_rate'] = $prodrates[$key];
            $this->ProductModel->add_products($data);
        }

    } 
}

您的模型add_products应该是这样的:

function add_products($data)
{
   if ( ! empty($data))
   {
      $this->db->insert('tbl_product_master', $data);
   }
}

答案 1 :(得分:0)

由于没有为shop_name列设置默认值“ NULL”,所以出现错误号1048。

如果该列可以采用NULL作为值,则使用显式DEFAULT NULL子句定义该列。

答案 2 :(得分:0)

您收到此错误,因为您尚未在数据库中将默认值设置为null, 转到表结构并将同一列的默认值更改为NULL,您将不会收到此错误 请参阅示例图像以供参考。

sampleimage