我想插入項目到數據庫中,它不工作。結帳按鈕會將我重定向到結帳頁面,但頁面上的所有信息都不會保存/插入到數據庫中。我不知道是什麼原因造成的。插入多個項目到數據庫不工作codeigniter
控制器:
<?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將'array'或'object'作爲插入參數。 https://www.codeigniter.com/userguide3/database/query_builder.html你是否嘗試過一個對象而不是數組或試圖填充循環內的所有值,並使用'$ this-> db- > insert_batch()'? –