2017-06-14 83 views
-1

我想插入項目到數據庫中,它不工作。結帳按鈕會將我重定向到結帳頁面,但頁面上的所有信息都不會保存/插入到數據庫中。我不知道是什麼原因造成的。插入多個項目到數據庫不工作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> 

感謝您的幫助

+0

你的代碼看起來很好,CI將'array'或'object'作爲插入參數。 https://www.codeigniter.com/userguide3/database/query_builder.html你是否嘗試過一個對象而不是數組或試圖填充循環內的所有值,並使用'$ this-> db- > insert_batch()'? –

回答

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

讓我知道,如果它不起作用。

0

也許你應該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); 
+0

我得到這個錯誤「insert_batch()調用沒有數據」 –

+0

您是否在批量插入之前使用'print_r($ data);'獲得正確的數據數組? –

+0

您的'inventoryID'數組可能沒有從0開始的鍵,因此使用'foreach'是您的解決方案,但我仍然會將兩個答案合併到您的案例中,並且只使用一個查詢將數據插入數據庫。現在嘗試一下,看看它是如何發展的,也許你會希望隨時檢查一下,看看哪種方式更快。在一個插入的大數組的循環中插入多個插入? –