Php 如何使用循环在Codeigniter中添加多个数据?
大家好,我只是需要一点帮助,关于使用循环在我的表中插入信息。 我不知道我的错误在哪里 这是我的代码:在我的插入模型中Php 如何使用循环在Codeigniter中添加多个数据?,php,codeigniter,Php,Codeigniter,大家好,我只是需要一点帮助,关于使用循环在我的表中插入信息。 我不知道我的错误在哪里 这是我的代码:在我的插入模型中 $poid = $this->input->post('po_id'); $cash_delivery = $this->input->post('cash_delivery'); $cash_check = $this->input->post('cash_check'); $b
$poid = $this->input->post('po_id');
$cash_delivery = $this->input->post('cash_delivery');
$cash_check = $this->input->post('cash_check');
$bank_transfer = $this->input->post('bank_transfer');
$itemname = $this->input->post('item'); // array of item name
$quantity = $this->input->post('qty'); // array of quantity
$price = $this->input->post('price'); //array of price
$total = $this->input->post('total'); //array of total
//CHECK IF TRANSACTION IS CHECKED
$val_delivery = NULL;
$val_check = NULL;
$val_transfer = NULL;
if(isset($_POST['cash_delivery'])){
$val_delivery = 'Y';
}else{
$val_delivery = 'N';
}
if(isset($_POST['cash_check'])){
$val_check = 'Y';
}else{
$val_check = 'N';
}
if(isset($_POST['bank_transfer'])){
$val_transfer = 'Y';
}else{
$val_transfer = 'N';
}
$filtername = array_filter($itemname);
$filterquantity = array_filter($quantity);
$filterprice = array_filter($price);
$filtertotal = array_filter($total);
//INSERT ORDERS
for($x = 0; $x < sizeof($filtername); $x++){
$orders = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filtertotal[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
$this->db->insert('po_order',$orders); //Only first item (index[0]) is added
echo "<pre>";
print_r($orders); //This will print my array values 'NO ERROR HERE'
echo "<hr />";
}
My database table design:
mysql> desc po_order;
+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| poid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| order_id | varchar(50) | NO | UNI | 0 | |
| item_desc | varchar(50) | NO | | NULL | |
| item_qty | int(10) unsigned | NO | | NULL | |
| item_price | float(7,2) unsigned | NO | | NULL | |
| total | float(7,2) unsigned | NO | | NULL | |
| cash_on_delivery | enum('Y','N') | NO | | NULL | |
| is_check | enum('Y','N') | NO | | NULL | |
| bank_transfer | enum('Y','N') | NO | | NULL | |
| transaction_date | datetime | NO | | NULL | |
+------------------+---------------------+------+-----+---------+----------------+
$poid=$this->input->post('po_id');
$cash\u delivery=$this->input->post('cash\u delivery');
$cash\u check=$this->input->post('cash\u check');
$bank_transfer=$this->input->post('bank_transfer');
$itemname=$this->input->post('item');//项目名称数组
$quantity=$this->input->post('quantity');//数量数组
$price=$this->input->post('price')//价格数组
$total=$this->input->post('total')//总数数组
//检查事务是否已检查
$val_delivery=NULL;
$val_check=NULL;
$val_transfer=NULL;
如果(isset($_POST['cash_delivery'])){
$val_交货='Y';
}否则{
$val_交货='N';
}
如果(isset($_POST['cash_check'])){
$val_check='Y';
}否则{
$val_check='N';
}
如果(isset($_POST[‘银行转账’])){
$val_transfer='Y';
}否则{
$val_transfer='N';
}
$filtername=array\u filter($itemname);
$filterquantity=数组\过滤器($quantity);
$filterprice=阵列过滤器($price);
$filtertotal=数组\过滤器($total);
//插入订单
对于($x=0;$xnull,
“订单id”=>poid美元,
'item_desc'=>$filtername[$x],
“物料数量”=>$filterquantity[$x],
“项目价格”=>$filtertotal[$x],
“总计”=>$filtertotal[$x],
“货到付款”=>val交货,
'is_check'=>$val_check,
“银行转账”=>$val\U转账,
“交易日”=>$dateorder
);
$this->db->insert('po_order',$orders);//只添加第一项(索引[0])
回声“;
print_r($orders);//这将打印我的数组值“此处无错误”
回声“
”;
}
我的数据库表设计:
mysql>描述订单;
+------------------+---------------------+------+-----+---------+----------------+
|字段|类型|空|键|默认|额外|
+------------------+---------------------+------+-----+---------+----------------+
|poid | int(10)无符号| NO | PRI | NULL |自动增量|
|订单号:varchar(50)|否| UNI | 0 ||
|项目描述varchar(50)|否| |空||
|物料|数量|整数(10)未签字|否|空||
|项目|价格|浮动(7,2)未签字|否|空||
|总计|浮点(7,2)无符号|无|空||
|货到付款| enum('Y','N')| NO | | NULL ||
|是|检查|枚举('Y','N')|否|空||
|银行转账| enum('Y','N')| NO | | NULL ||
|交易|日期|日期时间|否|空||
+------------------+---------------------+------+-----+---------+----------------+
我的问题是只添加了循环中的第一项。我无法从循环中获取下一个项目IMHO,您没有从$itemname=$this->input->post('item')中获取正确的值//数组应该返回,对吗 试着看看是否真的如此 也适用于您的for($x=0;$x
die('for循环将迭代“'.sizeof($filtername)。““times”);
IMHO,您没有从$itemname=$this->input->post('item')中获得正确的值;//数组应该返回正确吗
试着看看是否真的如此
也适用于您的for($x=0;$xdie('for循环将迭代“'.sizeof($filtername)。'“times”);
关于$itemname=$this->input->post('item')的确定我得到了包含输入数据的数组。我认为数组中存在错误。关于sizeof($filtername),它给我的for循环将迭代“2”次,因为在我的数组名中,我只输入了2个字段。我可以看到,您在将数组属性“poid”=>null中的主键“poid”自动递增为null时,将数据库写得太多了。您的数据库中一定存在双主键问题。因此,只需删除$itemname=$this->input->post('item')的问题我得到了包含输入数据的数组。我认为数组中存在错误。它给我的for循环的大小($filtername)将重复“2”次,因为在我的数组名称中,我只输入了2个字段。我可以看到,您正在过度编写数据库自动递增主键“poid”在数组属性“poid”=>null中,设置为null。您的数据库中必须存在双主键问题。因此,只需将其删除即可