繁体   English   中英

如何使用 codeigniter 4 框架在 mysql 数据库中插入多行?

[英]How to insert multiple rows in mysql database with codeigniter 4 framework?

我正在尝试使用 CodeIgniter 4 框架制作 web 应用程序,我需要在单个提交中插入多行。 我尝试使用 codeIgniter 4 查询生成器 insertBatch 并在 MySQL 数据库中插入多行时出现此错误。

错误:ErrorException 未定义索引:customerId

看法:

    <?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(); ?>

我的 function 在 Controller 下:

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

    }
}

Model:

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" } }

卖出表结构如下: 在此处输入图像描述

您的$data变量未正确设置为插入批次。 数组的每一行都必须对应于您要插入的一个新 object。

如果您的键是默认键并且$tmp_data的每一行具有相同的长度,这应该可以工作。

$data = [];
$tmp_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']
];
foreach ($tmp_data as $k => $v) {
    for($i = 0; $i < count($v);$i++) {
        $data[$i][$k] = $v[$i];
    }
}

请务必查看文档: https://codeigniter.com/user_guide/database/query_builder.html?highlight=insertbatch

暂无
暂无

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

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