douduidui1046 2017-06-14 16:11
浏览 73
已采纳

将多个项插入数据库而不是工作的codeigniter

I am trying to insert items into the database and it's not working. The checkout button redirects me to the checkout page but none of the information on the page is saved/inserted into the database. I don't know what's causing it.

Controller:

<?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');
}
} ?>

View:

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

Thanks for your help

  • 写回答

2条回答 默认 最新

  • drjun1994 2017-06-14 16:35
    关注

    I can see error in your code.

    When you use array field you can simply use like describe below.

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

    Let me know if it not works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误