繁体   English   中英

如何使用Codeigniter以动态形式实现插入查询

[英]how to implement insert query in a dynamic form using codeigniter

我为存储在数据库中的数据的多次插入创建了一个动态表单,这里有两个表'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
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);
   }
}

错误编号:1048,因为您没有为shop_name列设置默认值“ NULL”,所以出现此shop_name

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

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

sampleimage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM