2017-06-06 370 views
0

我試圖插入數據到數據庫中,並且我的代碼添加了一個新行但所有值爲空。我不知道是什麼原因造成的。該視圖是在html中的模式。這裏是我的代碼:感謝您的幫助插入在codeigniter中不能正常工作

控制器:

public function addItem(){ 
    $save = array(
     'inventoryID' => $this->input->post('rfid'), 
     'masterCode' => $this->input->post('masterCode'), 
     'itemName' => $this->input->post('itemName'), 
     'colorName' => $this->input->post('colorName'), 
     'location' => $this->input->post('location'), 
     'itemCategory' => $this->input->post('itemCategory'), 
     'materialDescription' => $this->input->post('materialDescription'), 
     'supplier' => $this->input->post('supplier'), 
     'itemDescription' => $this->input->post('itemDescription'), 
     'comments' => $this->input->post('comments'), 
     'itemCode' => $this->input->post('itemCode'), 
     'colorCode' => $this->input->post('colorCode') 
    ); 
    $this->searchModel->form_insert($save); 

    //load the header 
    $this->load->view('base.php',$save); 
    //load the page 
    redirect('Search'); 
    //load the footer 
    $this->load->view('footer.php',$save); 
} 

型號:

功能form_insert($數據){

// Inserting in Table(inventory) of Database(library) 
    $this->load->database(); 
    $this->db->insert('inventory', $data); 
    $inventoryID = $this->db->insert_id(); 
} 

查看:

<div id="addItem" class="modal fade"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <form method ="post" action= "<?php echo site_url("Search/addItem"); ?>"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
      <h4 class="modal-title">Add an Item</h4> 
     </div> 
     <div class="modal-body"> 
      <form role="form"> 
      <table> 
       <tr> 
       <td><input type="text" name="rfid" placeholder="RFID" required/></td> 
       <td><input type="text" name="itemCode" placeholder="Item Code" required/></td> 
       <td><input type="text" name="masterCode" placeholder="Master Code" /></td> 
       </tr> 
       <tr> 
       <td><input type="text" name="itemName" placeholder="Item Name" required/></td> 
       <td><input type="text" name="colorCode" placeholder="Color Code" /></td> 
       <td><input type="text" name="colorName" placeholder="Color Name" /></td> 
       </tr> 
       <tr> 
       <td><input type="text" name="location" placeholder="Location" required/></td> 
       <td><input type="text" name="makelocation" placeholder="Location Made" required/></td> 
       <td><input type="text" name="itemCategory" placeholder="Item Category" /></td> 
       </tr> 
       <tr> 
       <td><input type="text" name="materialDescription" placeholder="Material Description" /></td> 
       <td><input type="text" name="supplier" placeholder="Supplier/Vendor" required/></td> 
       <td><input type="text" name="checkoutAllowed" placeholder="Checkout Allowed" /></td> 
       </tr> 
      </table> 
      <div class="row personal-info"> 
       <div class="col-sm-4"> 
       <div class="form-group"> 
        <textarea name="itemDescription" placeholder="Insert information regarding the weather this item is suitable for and where it is used"></textarea> 
        <textarea name="Comments" placeholder="Additional Coments on the Item"></textarea> 
       </div> 
       </div> 
      </div> 
      </form> 
     </div> 
     <div class="modal-footer" style="text-align:center;"> 
      <a href="<?php echo site_url("Search/addItem") ?>"><input class="btn btn-primary" name="addItem" value="Add Item"></a> 
     </div> 
     </form> 
    </div> 
    </div> 
</div> 
+0

public function addItem(){print_r($ this-> input-> post()); die(); 然後檢查 –

+0

表中的列名稱是什麼? –

回答

1

問題是,你這樣做實際上不是「提交」表單數據。您的按鈕鏈接到正確的控制器/方法,但在使用鏈接時不會將任何數據發送到控制器,即<a href=...。你需要一個提交按鈕。

這個改變很簡單。如下更改按鈕的代碼。

<div class="modal-footer" style="text-align:center;"> 
    <input type="submit" class="btn btn-primary" name="addItem" value="Add Item"> 
</div> 

還有一個問題。您有兩個<form>標籤。 刪除行

<form method ="post" action="<?php echo site_url("Search/addItem"); ?>"> 

,並更改行

<form role="form"> 

<form method="post" action="<?php echo site_url("Search/addItem"); ?>" role="form"> 

您還需要刪除兩行按鈕的代碼上面多餘的形式結束標記。不允許嵌套<form>s。並且您需要移動結束標記<div class="modal-content">因此,爲了更清楚地說明您的視圖最終應該是什麼樣子的。

<div id="addItem" class="modal fade"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
     <h4 class="modal-title">Add an Item</h4> 
     </div> 
     <div class="modal-body"> 
     <form method ="post" action= "<?php echo site_url("Search/addItem"); ?>" role="form"> 
      <table> 
      <tr> 
       <td><input type="text" name="rfid" placeholder="RFID" required/></td> 
       <td><input type="text" name="itemCode" placeholder="Item Code" required/></td> 
       <td><input type="text" name="masterCode" placeholder="Master Code" /></td> 
      </tr> 
      <tr> 
       <td><input type="text" name="itemName" placeholder="Item Name" required/></td> 
       <td><input type="text" name="colorCode" placeholder="Color Code" /></td> 
       <td><input type="text" name="colorName" placeholder="Color Name" /></td> 
      </tr> 
      <tr> 
       <td><input type="text" name="location" placeholder="Location" required/></td> 
       <td><input type="text" name="makelocation" placeholder="Location Made" required/></td> 
       <td><input type="text" name="itemCategory" placeholder="Item Category" /></td> 
      </tr> 
      <tr> 
       <td><input type="text" name="materialDescription" placeholder="Material Description" /></td> 
       <td><input type="text" name="supplier" placeholder="Supplier/Vendor" required/></td> 
       <td><input type="text" name="checkoutAllowed" placeholder="Checkout Allowed" /></td> 
      </tr> 
      </table> 
      <div class="row personal-info"> 
      <div class="col-sm-4"> 
       <div class="form-group"> 
       <textarea name="itemDescription" placeholder="Insert information regarding the weather this item is suitable for and where it is used"></textarea> 
       <textarea name="Comments" placeholder="Additional Coments on the Item"></textarea> 
       </div> 
      </div> 
      </div> 
      <div class="modal-footer" style="text-align:center;"> 
      <input type="submit" class="btn btn-primary" name="addItem" value="Add Item"> 
      </div> 
     </form> 
     </div> 
    </div> 
    </div> 
</div> 
+0

感謝您的幫助! –