使用 codeigniter 将多条记录插入到表中 [英] insert multiple record to a table with codeigniter
问题描述
我想使用 codeigniter 将记录(我从表中获得的)插入到另一个表中.这是添加记录的功能.我通过 $nokw 作为外键插入到另一个表中.:
I want to insert the records (that i got from a table) to another table using codeigniter. here's the function to add the record. I pass the $nokw to insert to another table as foreign key.:
function add_detail($nokw){
$id_sj = $this->session->userdata('id');
$upddate = date('Y')."-".date('m')."-".date('d')." ".date('H').":".date('i').":".date('s');
$i=0;
$this->suratjalan->where('IDDeliveryNo',$id_sj);
$rec = $this->suratjalan->get("t02deliveryno_d")->result_array();
// parse the result and insert it into an array
foreach ($rec as $det){
$i++;
$detail[$i] = array(
'ID' => '',
'NoKwitansi' => $nokw,
'TypeProduct'=> $det['TypeProduct'],
'PartNo' => $det['PartNo'],
'PartNoVendor'=> $det['PartNoVendor'],
'SerialPanel' => $det['SerialPanel'],
'Description' => $det['Description'],
'Dimension' => $det['Dimension'],
'DescriptionVendor' => $det['DescriptionVendor'],
'DimensionVendor' => $det['DimensionVendor'],
'PrintedProduct' => $det['PrintedProduct'],
'Qty' => $det['Qty'],
'UoM' => $det['UoM'],
'Remark' => $det['Remark'],
'UpdUser'=> $this->session->userdata('user'),
'UpdDate'=> $upddate
);
// insert the record
$this->finance->insert('t02fkpd',$detail[$i]);
}
}
它有效,但如果从表 't02deliveryno_d' 返回多于一行,则无效.我认为插入记录时会出现错误.我使用 $i++
在 $detail
数组中创建不同的索引.
It works, but it doesn't work if more than one row is returned from the table 't02deliveryno_d'. I think the error comes when i insert the record. i use the $i++
to make different index in $detail
array.
如何解决此问题以正确插入多行?
How can I fix this to properly insert multiple rows?
推荐答案
您没有显示数据库架构,但我假设 t02fkpd.ID
是一个自动递增列.
You didn't show the db schema, but I'm assuming that t02fkpd.ID
is an auto-incrementing column.
如果是这种情况,问题在于您为 ID
指定了一个空白值,而不是让数据库处理它.这可能导致尝试插入具有相同(空白)ID 的重复行.
If that's the case, the problem is that you're specifying a blank value for ID
instead of letting the database handle it. This is probably resulting in attempts to insert duplicate rows with the same (blank) id.
这是您的函数的更新版本,我怀疑它会更好地工作:
Here's an updated version of your function that I suspect will work better:
function add_detail($nokw) {
$id_sj = $this->session->userdata('id');
$upddate = date('Y-m-d H:i:s');
$this->suratjalan->where('IDDeliveryNo',$id_sj);
$rec = $this->suratjalan->get("t02deliveryno_d")->result_array();
foreach ($rec as $det) {
$details = array(
'NoKwitansi' => $nokw,
'TypeProduct'=> $det['TypeProduct'],
'PartNo' => $det['PartNo'],
'PartNoVendor'=> $det['PartNoVendor'],
'SerialPanel' => $det['SerialPanel'],
'Description' => $det['Description'],
'Dimension' => $det['Dimension'],
'DescriptionVendor' => $det['DescriptionVendor'],
'DimensionVendor' => $det['DimensionVendor'],
'PrintedProduct' => $det['PrintedProduct'],
'Qty' => $det['Qty'],
'UoM' => $det['UoM'],
'Remark' => $det['Remark'],
'UpdUser'=> $this->session->userdata('user'),
'UpdDate'=> $upddate
);
$this->finance->insert('t02fkpd',$details);
}
}
除了删除 ID
值,我还做了以下小改动:
Beyond removing the ID
value, I also made the following minor changes:
我删除了
$i
并重用了相同的变量来构建要插入的值数组.插入后您没有使用数组,因此无需构建所有行的列表 - 您可以每次都覆盖它.
I removed
$i
and just reused the same variable for building the array of values to insert. You're not using the array after you insert, so there's no need to build a list of all the rows - you can just overwrite it each time.
我将您的 $upddate
计算更改为仅调用 date()
一次.您可以在一次调用中指定整个格式字符串 - 一次不限于一个字符.
I changed your the $upddate
calculation to only call date()
once. You can specify an entire format string in one call - you're not restricted to just a single character at a time.
这篇关于使用 codeigniter 将多条记录插入到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!