插入在codeigniter中无法正常工作

时间:2017-06-06 13:45:01

标签: php codeigniter

我正在尝试将数据插入数据库,而我的代码添加新行,但所有值都为空。我不知道是什么导致了它。视图位于html中的模态内。这是我的代码:感谢您的帮助

控制器:

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

型号:

function form_insert($ data){

    // 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>

1 个答案:

答案 0 :(得分:1)

问题是你实际上并没有提交&#34;表格数据。您的按钮链接到正确的控制器/方法,但在使用链接时不会向控制器发送任何数据,即<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>