gpt4 book ai didi

php - 如何使用codeigniter以动态形式实现插入查询

转载 作者:行者123 更新时间:2023-11-29 16:30:53 25 4
gpt4 key购买 nike

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

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

关于php - 如何使用codeigniter以动态形式实现插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com