2014-02-05 82 views
0

我是Codeigniter中的新手。我無法將數據插入到我的表格中。我也使用過引導文件。無法在codeigniter中插入數據

這些是查看線路 - groups.php

<input type="text" name="group" id="group" placeholder="Group"> 
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button> 

型號 - groups_model.php

function insert_groups($dbdata) { 
     $this->db->insert('groups', $dbdata); 
    } 

控制器 - groups_controller.php

public function group() 
    { 
     $this->load->view('groups'); 
     $this->load->model('groups_model'); 
     if(isset($_POST['addgroup'])) 
     { 
      $userID= '2'; 
      $groupname= $this->input->post('group'); 
      //$this->load->model('groups_model'); 
      $data=array(
       'userID'=>$userID, 
       'groupname'=>$groupname 
      ); 
      $this->groups_model->insert_groups($data); 
      //var_dump($this->db->last_query()); 
     } 

    } 
} 

我的全HTML表格粘貼下面。那裏有很多評論。

<!DOCTYPE html> 
<!-- 
To change this license header, choose License Headers in Project Properties. 
To change this template file, choose Tools | Templates 
and open the template in the editor. 
--> 
<html> 
    <head> 
     <title>Content</title> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <!-- Bootstrap --> 
     <link href="<?php echo base_url(); ?>stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css"> 
     <link href="<?php echo base_url(); ?>stylesheets/bootstrap.css" rel="stylesheet" type="text/css"> 
     <link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css"> 
     <link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css"> 
     <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
     <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> 
     <![endif]--> 
     <SCRIPT language="javascript"> 

//   var person; 
//   var element; 
// 
//   function add() { 
//    var e1 = document.getElementById('group').value; 
//    var e2 = document.getElementById('select'); 
//    var o = document.createElement('option'); 
//    o.value = "group_body.html"; 
//    o.text = e1; 
//    e2.options.add(o); 
// 
//   } 

//   function goto() { 
//    var index = document.getElementById('select').selectedIndex; 
//    if (index.value != "select") { 
//     location = index.value; 
//    } 
//   } 
//   function add() 
//   { 
//    //Create an input type dynamically. 
//    element = document.createElement("button"); 
//    element.className='btn btn-default'; 
//    var person = document.getElementById('group').value; 
//    var t = document.createTextNode(person); 
//    element.appendChild(t); 
//    element.id=person; 
//    //Assign different attributes to the element. 
////     element.type = type; 
////     element.value = type; // Really? You want the default value to be the type string? 
////     element.name = type; // And the name too? 
//     
//    element.onclick = function() { // Note this is a function 
//     //alert(element.id); 
////     $(document).ready(function(){ 
////     $("#element").click(function(){ 
//      $('body').load('group_body.html'); 
////     }); 
////   }); 
//    }; 
// 
//    var foo = document.getElementById("fooBar"); 
//    //Append the element in page (in span). 
//    foo.appendChild(element); 
////     var d = document.getElementById('fooBar'); 
////     d.appendChild(i); 
//   } 
// 
//   function copy() 
//   { 
//    var n1 = document.getElementById('addKeyword'); 
//    var n2 = document.getElementById('getKeyword'); 
//    n2.innerText = n1.value; 
//   } 

     </SCRIPT> 
    </head> 
    <body> 
     <form method="post" name="form1"> 
     <div class="btn-group" id="fooBar"> 
      <!--   <button type="button" class="btn btn-default">Marketing</button> 
         <button type="button" class="btn btn-default">Internet</button> 
         <button type="button" class="btn btn-default">Politics</button>--> 

      select a group: 
      <select class="form-control" id="select" onchange="javascript:location.href = this.value;"> 
       <option selected value="select">select</option> 
       <!--    <option>2</option> 
           <option>3</option> 
           <option>4</option> 
           <option>5</option>--> 
      </select> 
      <button type="button" class="btn btn-default" id="btnAdd" data-toggle="modal" data-target="#myModal">Add Group</button> 
     </div> 
     <!-- Modal --> 
     <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         <h4 class="modal-title" id="myModalLabel">Group Name</h4> 
        </div> 
        <div class="modal-body"> 
         <h5>Please enter Group Name:</h5> 
         <input type="text" name="group" id="group" placeholder="Group"> 
        </div> 
        <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
         <button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button> 
        </div> 
       </div><!-- /.modal-content --> 
      </div><!-- /.modal-dialog --> 
     </div><!-- /.modal --> 
     <div class="boxed"> 
      <h4>Group:<span class="label label-default" id="span"><script>document.getElementById('group').innerText = document.getElementById('span').innerText</script></span> 
       <button type="submit" class="btn btn-default">Edit Name</button> 
       <button type="submit" class="btn btn-default">Disable</button> 
       <button type="submit" class="btn btn-default">Delete Group</button></h4> 
      <div class="row"> 
       <div class="col-lg-6"> 
        <div class="input-group"> 
         <input type="text" class="form-control" placeholder="Enter your keyword" id="addKeyword"> 
         <span class="input-group-btn"> 
          <button class="btn btn-default" type="button" onclick="copy()">Add</button> 
         </span> 
        </div><!-- /input-group --> 
       </div><!-- /.col-lg-6 --> 
      </div> 
      <h4>Keywords:</h4> 
      <!--   <div class="keyword" id="getKeyword"></div>--> 
     </div> 
     </form> 

     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
     <script src="https://code.jquery.com/jquery.js"></script> 
     <!-- Include all compiled plugins (below), or include individual files as needed --> 
     <script src="<?php echo base_url(); ?>javascripts/bootstrap.min.js"></script> 
     <script src="<?php echo base_url(); ?>javascripts/bootstrap.js"></script> 
    </body> 
</html> 
+0

last_query()是什麼?你檢查查詢嗎? –

+0

當前我的查看頁面不顯示。這是說沒有找到項目名稱/組。在路由中,我通過$ route ['groups'] = groups_controller/group指定了它。 – upjc

+0

將根改爲'$ route ['groups'] = groups/group' –

回答

0

爲什麼在應該提交時使用按鈕。

改變這種

<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button> 

<button type="submit" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button> 

你必須提交數據,以得到它後

+0

更改了它,但沒有發生插入。 – upjc

+0

你有表單標籤或form_open標籤嗎? –

+0

我有表格標籤。我在問題末尾附上了完整的groups.php(查看文件)。 – upjc

2

嘗試

public function group() 
    { 

     $this->load->model('groups_model'); 
     if($this->input->post('addgroup')) 
     { 
      $userID= '2'; 
      $groupname= $this->input->post('group'); 

      $data=array(
       'userID'=>$userID, 
       'groupname'=>$groupname 
      ); 
      $this->groups_model->insert_groups($data); 
        } 
$this->load->view('groups'); 
    } 
} 

並添加動作到您的形式:
變化

<form method="post" name="form1"> 

<?= form_open('yourcontroller/action')?> 
//anything HTML inside your form 
<?= form_close()?> 

而且你必須加載形式幫助做到這一點。在autoload.php

$autoload['helper'] = array('form'); 

希望它有幫助!

+0

中提交類似於提交按鈕的類,而不使用form_open並關閉它。不管怎麼說,還是要謝謝你。 – upjc

0

基本上,你的表單標記中沒有action屬性。

<form method="post" name="form1"> 

您可以使用此表達式:

<form action="yourcontroller/action" method="post" name="form1"> 

或使用笨表單輔助

$this->load->helper('form'); 
echo form_open("yourcontroller/action"); 

希望它可以幫助你。