我试图使用CodeIgniter 4框架制作一个Web应用程序,其中我需要在一次提交中插入多行。我尝试使用codeIgniter 4查询构建器insertBatch,在MySQL数据库中插入多行时出现此错误。
错误:ErrorException未定义索引:客户ID
浏览次数:
<?php $this->extend('dashboard'); ?>
<?php $this->section('content'); ?>
<script type="text/javascript">
$(document).ready(function(){
var html ='<tr><td><input type="text" name="customerId[]" value="<?=$customerId?>"></td><td><input type="text" name="transactionType[]" value="sell"></td><td><select name="productName[]"><?php
foreach ($productsInfo as $product) {
echo '<option value="'.$product['productName'].'">'.$product['productName'].'</option>';
}
?>
</select></td><td><input type="text" name="quantity[]"></td><td><input type="text" name="unit[]"></td><td><input type="text" name="price[]"></td><td><input type="text" name="payment[]"></td><td><input type="date" name="sellDate[]"></td><td><input type="button" class="btn btn-primary" id="remove" value="remove" name="remove"></td></tr>';
$("#add").click(function(){
$("#sellForm").append(html);
});
$("#sellForm").on('click','#remove',function(){
$(this).closest('tr').remove();
});
});
</script>
<form class="p-2" method="post" action="<?=site_url('admin/sellDataSave')?>">
<div class="table-responsive">
<table class="table" id="sellForm">
<tr class="bg-info text-center">
<th>ID</th>
<th>Type</th>
<th>Product</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price</th>
<th>Payment</th>
<th>Date</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="customerId[]" value="<?=$customerId?>"></td>
<td><input type="text" name="transactionType[]" value="sell"></td>
<td>
<select name="productName[]">
<?php
foreach ($productsInfo as $product) {
echo '<option value="'.$product['productName'].'">'.$product['productName'].'</option>';
}
?>
</select>
</td>
<td><input type="text" name="quantity[]"></td>
<td><input type="text" name="unit[]"></td>
<td><input type="text" name="price[]"></td>
<td><input type="text" name="payment[]"></td>
<td><input type="date" name="sellDate[]"></td>
<td><input type="button" class="btn btn-primary" id="add" value="add" name="add"></td>
</tr>
</table>
<center>
<input type="submit" value="Submit" class="btn btn-primary">
</center>
</div>
</form>
<?php $this->endSection(); ?>
我在主计长下的职务:
public function sellDataSave()
{
$session = session();
if (!$session->has('username'))
{
return $this->response->redirect(site_url('home'));
}
else
{
$data=[
'customerId'=>$_POST['customerId'],
'transactionType'=>$_POST['transactionType'],
'productName'=>$_POST['productName'],
'quantity'=>$_POST['quantity'],
'unit'=>$_POST['unit'],
'price'=>$_POST['price'],
'payment'=>$_POST['payment'],
'sellDate'=>$_POST['sellDate']
];
$sellModel=new Sell();
$sellModel->insertBatch($data);
}
}
型号:
use CodeIgniter\Model;
class Sell extends Model
{
protected $table = 'sell';
protected $primaryKey = 'sellId';
protected $allowedFields = ['customerId', 'sellId','transactionType','productName','quantity','price','payment','sellDate'];
}
我提交了两行,在vardump($data)后得到了这个值。
array(8) { ["customerId"]=> array(2) { [0]=> string(2) "13" [1]=> string(2) "13" } ["transactionType"]=> array(2) { [0]=> string(4) "sell" [1]=> string(4) "sell" } ["productName"]=> array(2) { [0]=> string(19) "Poultry Feed Edited" [1]=> string(19) "Poultry Feed Edited" } ["quantity"]=> array(2) { [0]=> string(2) "67" [1]=> string(2) "78" } ["unit"]=> array(2) { [0]=> string(2) "kg" [1]=> string(2) "kg" } ["price"]=> array(2) { [0]=> string(4) "8996" [1]=> string(4) "8678" } ["payment"]=> array(2) { [0]=> string(4) "7896" [1]=> string(4) "7654" } ["sellDate"]=> array(2) { [0]=> string(10) "2020-05-14" [1]=> string(10) "2020-05-14" } }
销售表结构如下所示:
2条答案
按热度按时间vlju58qv1#
您的
$data
变量没有为插入批处理正确设置。数组的每一行都必须对应于您要插入的一个新对象。如果您的键是默认键,并且
$tmp_data
的每一行都具有相同的长度,那么这应该可以工作。请务必查看文档:https://codeigniter.com/user_guide/database/query_builder.html?highlight=insertbatch
b91juud32#
将“多行”数据收集为单独的数组,即“$array_1,$array_2”等;然后将包含所有行的'
$data
'数组作为参数传递给CodeIgniter db函数'insertBatch()
'。insertBatch()将插入每行。请参阅:https://codeigniter.com/user_guide/database/query_builder.html?highlight=insert#insertbatch