Saadia Surti
Saadia Surti

Reputation: 59

inserting multiple item into the database not working 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

Upvotes: 0

Views: 658

Answers (2)

user2560539
user2560539

Reputation:

Perhaps you should build your database INSERT data first and then execute one query only using insert_batch() instead of multiple insert() inside your loop. Your inventoryID array does not have keys matching 0 to $count and produce an error inside the loop leaving your $data array empty. Using foreach solves this issue.

So instead of

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

Could you try

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

Upvotes: 0

Alex Mac
Alex Mac

Reputation: 2993

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.

Upvotes: 1

Related Questions