【问题标题】:inserting multiple item into the database not working codeigniter将多个项目插入数据库不起作用codeigniter
【发布时间】:2017-06-14 16:11:21
【问题描述】:

我正在尝试将项目插入数据库,但它不工作。结帐按钮将我重定向到结帐页面,但页面上的任何信息都没有保存/插入到数据库中。不知道是什么原因造成的。

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CheckOut extends CI_Controller {
function __construct() {
    parent::__construct();
    // Load url helper
}
public function index(){
    $this->load->helper('url');

    $this->load->view('base');
    $this->load->view('checkOut');
}
function insert(){
    $adminID=$this->input->post('adminID');
    $customerID=$this->input->post('customerID');
    $dateOut=$this->input->post('dateOut');
    $dateDue=$this->input->post('dateDue');
    $inventoryID=$this->input->post('inventoryID');
    $count = count($this->input->post['inventoryID']);
    for($i=0; $i<$count; $i++) {
        $data = array(
            'inventoryID' => $inventoryID[$i], 
            'adminID' => $adminID,
            'customerID' => $customerID,
            'dateOut' => $dateOut,
            'dateIn' => $dateDue,
        );
        print_r($data);
        $this->db->insert('loan', $data);
    }
    $this->load->view('base');
    redirect('checkOut/index');
}
} ?>

查看:

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>checkIn.css">
    <title>Check Out Items</title>
</head>
<body>
    <h1><center>Check Out Items</center></h1><hr>
    <div class="container">
        <form class="form-horizontal" method='post' role="form" data-parsley-validate="" id="checkOut" action="<?php echo site_url("checkOut/insert"); ?>">
             <div class="row personal-info" id="checkOutForm">    
                <div class="col-sm-4">
                    <div class="form-group">
                        <label>Administrator ID:</label>
                        <input type="text" class="form-control" id="adminID" name="adminID" placeholder="Admin ID">
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="form-group">
                        <label>Customer ID:</label>
                        <input type="text" class="form-control" id="customerID" name="customerID" placeholder="Customer ID">
                    </div>
                </div>
                 <div class="col-sm-4">
                    <div class="form-group">
                        <label>Today's Date:</label>
                        <input type="date" class="form-control" id="dateOut" name="dateOut" placeholder="Date Out">
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="form-group">
                        <label>Due Date:</label>
                        <input type="date" class="form-control" id="dateDue" name="dateDue" placeholder="Date Due">
                    </div>
                </div>
                 <div class="col-sm-4">
                     <div class="form-group">
                        <label>Inventory ID:</label>
                        <div class="input_fields_wrap">
                            <div><input type="text" name="inventoryID[]" placeholder="RFID">
                                <button class="add_field_button">Add More Fields</button></div>
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <div class="form-group" style="text-align:center;">
                <input class="btn btn-primary" type="submit" name="checkOut" value="Check Out">
            </div>
        </form>
    </div>
</body>
<script>
    $(document).ready(function() {
        var max_fields      = 10; //maximum input boxes allowed
        var wrapper         = $(".input_fields_wrap"); //Fields wrapper
        var add_button      = $(".add_field_button"); //Add button ID

        var x = 1; //initlal text box count
        $(add_button).click(function(e){ //on add input button click
            e.preventDefault();
            if(x < max_fields){ //max input box allowed
                x++; //text box increment
                $(wrapper).append('<div><input type="text" name="inventoryID[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
            }
    });

        $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });
</script>
</html>

感谢您的帮助

【问题讨论】:

  • 您的代码看起来不错,CI 将arrayobject 作为插入参数。 codeigniter.com/userguide3/database/query_builder.html您是否尝试过使用对象而不是数组或尝试填充循环内的所有值并使用$this-&gt;db-&gt;insert_batch()通过一个查询将其传递到数据库?

标签: php jquery mysql codeigniter


【解决方案1】:

我可以在您的代码中看到错误。

当你使用数组字段时,你可以像下面描述的那样简单地使用。

function insert(){
    $adminID=$this->input->post('adminID');
    $customerID=$this->input->post('customerID');
    $dateOut=$this->input->post('dateOut');
    $dateDue=$this->input->post('dateDue');
    $inventoryID=$this->input->post('inventoryID');
    $ids = $this->input->post('inventoryID');
    foreach($ids as $id):
          $data = array(
            'inventoryID' =>$id, 
            'adminID' => $adminID,
            'customerID' => $customerID,
            'dateOut' => $dateOut,
            'dateIn' => $dateDue,
        );
        $this->db->insert('loan', $data);
        $data = array();
    endforeach;
    $this->load->view('base');
    redirect('checkOut/index');
}

如果它不起作用,请告诉我。

【讨论】:

    【解决方案2】:

    也许您应该先构建数据库INSERT 数据,然后在循环中仅使用insert_batch() 而不是多个insert() 执行一个查询。 您的 inventoryID 数组没有匹配 0 到 $count 的键,并在循环内产生错误,使您的 $data 数组为空。使用foreach 解决了这个问题。

    所以不是

    for($i=0; $i<$count; $i++) {
        $data = array(
            'inventoryID' => $inventoryID[$i], 
            'adminID' => $adminID,
            'customerID' => $customerID,
            'dateOut' => $dateOut,
            'dateIn' => $dateDue,
        );
        print_r($data);
        $this->db->insert('loan', $data);
    }
    

    你可以试试

    $data = array();
    foreach($inventoryID as $v) {
        array_push($data, array(
            'inventoryID' => $v, 
            'adminID' => $adminID,
            'customerID' => $customerID,
            'dateOut' => $dateOut,
            'dateIn' => $dateDue,
        ));
    }
    print_r($data);
    $this->db->insert_batch('loan', $data);
    

    【讨论】:

    • 我收到此错误“insert_batch() 调用时没有数据”
    • 在批量插入之前使用print_r($data); 是否获得了正确的数据数组?
    • 您的 inventoryID 数组可能没有从 0 开始的键,因此使用 foreach 是您的解决方案,但我仍然会在您的情况下合并这两个答案并仅使用一个查询将数据插入数据库。现在尝试一下,看看效果如何,也许您想对照时间检查一下,看看哪种方式更快。一个插入中的大数组循环内的多个插入?
    猜你喜欢
    • 2021-10-31
    • 2018-08-11
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多