0

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

1
  • Your code seems fine, CI takes array or object as insert parameters. codeigniter.com/userguide3/database/query_builder.html Have you tried an object instead of array or trying to populate all values inside the loop and pass it to database with one query using $this->db->insert_batch()? Commented Jun 14, 2017 at 16:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I am getting this error "insert_batch() called with no data"
Do you get a correct array of data by using print_r($data); just before the batch insert?
your inventoryID array might not have keys starting from 0 so using foreach is your solution, but still i would merge both answers in your case and use only one query for insert data to database. Try it now and see how it goes, maybe you would like to check it against time in order to see which way is faster. Multiple inserts inside a loop of a large array in one insert?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.