2010-06-23 99 views
3

HI插入到數據庫中使用笨

我想插入CI使用形式的數據,但我面臨的問題 這裏是我的模型代碼

function add_form() { 
    $this->load->database(); 
    $id = $this->input->post('id'); 
    $name = $this->input->post('name'); 
    $age = $this->input->post('age');  
    $data = array(
     'name' => $this->input->post('name'), 
     'age' => $this->input->post('age'), 
    ); 
    $this->db->insert('user',$data); 
} 

這裏是我的控制器代碼

function simpleform() { 
    $this->load->helper('form'); 
    $this->load->helper('html'); 
    $this->load->model('welcomedb_model'); 
    if($this->input->post('submit')) { 
     $this->welcomedb_model->add_form(); 
    } 
    $this->load->view('welcomedb_view'); 
} 

這裏是我的視圖代碼

<?php echo form_open('welcomedb/submit'); ?> 
    <? echo $name; ?>: 
    <? echo form_input('name'); ?> 
    </br> 
    <? echo $age; ?>: 
    <? echo form_input('age'); ?> 
    </br> 
    <?php echo form_submit('submit', 'Submit'); ?> 
    <?php echo form_close(); ?> 

感謝您的幫助

+0

查看代碼:<?php echo form_open('welcomedb/submit'); ?> <? echo $ name; ?>: <? echo form_input('name'); ?>
<? echo $ age; ?>: <? echo form_input('age'); ?>
<?php echo form_submit('submit','Submit'); ?> <?php echo form_close(); ?> – webkul 2010-06-23 14:02:39

+1

請重新編輯您的問題並相應地設置代碼的格式。你很近,但第一次嘗試就錯過了。此外,您的視圖代碼應該在那裏,而不是在評論中。 – MJB 2010-06-23 14:24:21

+0

請重新格式化您的代碼。 – ggfan 2010-06-23 19:18:43

回答

10

您的表單正在提交到welcomedb/submit,但您的控制器似乎在welcomedb/simpleform ......也許您需要更改該表格。

否則,似乎沒有任何問題。

+0

我做過,但它不工作 – webkul 2010-06-24 06:58:39

+0

最後,我發現它的解決方案應該是<?php回聲form_open( 'welcomedb/simpleform'); ?> 感謝您的幫助 – webkul 2010-06-24 20:09:06

+3

顯示這個人一些信用..檢查他的答案 – mosid 2012-03-12 14:50:31

2

大概:

$data = array(
    'name' => $this->input->post('name'), 
    'age' => $this->input->post('age'), 
); 

從最後一個元素(年齡)這樣的刪除逗號:

$data = array(
    'name' => $this->input->post('name'), 
    'age' => $this->input->post('age') 
); 

,總是傾錯誤。

+0

它的PHP不JS! 向最後一個數組項添加逗號通常被認爲是很好的做法。 – 2014-05-02 07:06:44

1
Best way is to do these code ...... 
First conifg the autoload and database. 
into autoload:$autoload['libraries'] = array('database'); 
       $autoload['helper'] = array('url','form','file'); 
Into controller 

<?php 
    class CDemo extends CI_Controller 
    { 

     function index() 
     { 
      $this->load->view('VDemo'); 
     } 
     function save() 
     { 
      $this->load->model('MDemo'); 

      if($this->input->post('submit')) 
      { 
       $this->MDemo->process();     
      } 
      redirect('CDemo'); 
     } 
    } 
?> 

Into Model 
<?php 
    class MDemo extends CI_Model 
    { 
     function process() 
     { 
      $Id = $this->input->post('Id'); 
      $Name = $this->input->post('Name'); 
      $data = array(
        'Id'=>$Id, 
        'Name'=>$Name      
        ); 
        $this->db->insert('test',$data);  
      } 
     } 
?> 

Into View 
<html> 
<head> 
<title>DEMO</title> 
</head> 
<body> 
    <h1>Form Biodata</h1> 
    <?php 
     echo form_open('CDemo/save', array('name' => 'VDemo'));  
    ?> 
     <table> 
      <tr> 
       <td>Id :</td> 
       <td><input type="text" name="Id"></input></td> 
      </tr> 
      <tr> 
       <td>Name :</td> 
       <td><input type="text" name="Name"></input></td> 
      </tr>  
      <tr> 
       <td><input type="submit" name="submit" value="submit"></input></td> 
      </tr>   
     </table> 
    <?php 
     echo form_close(); 
    ?> 
    <textarea rows="" cols="">Hello</textarea> 
</body> 
</html>