2010-06-23 1 views
3

HI내가 CI에서 양식을 사용하여 데이터를 삽입 할 CodeIgniter의에게

를 사용하여 데이터베이스에 삽입하지만 난 여기에 문제 에 직면하고 나의 컨트롤러 코드

내 모델 코드 여기

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

코드보기 : :
:
webkul

+1

질문을 다시 편집하고 그에 따라 코드의 형식을 지정하십시오. 너는 가깝지만 첫 번째 시도에서 놓쳤다. 또한보기 코드가 주석이 아닌 주석에 있어야합니다. – MJB

+0

코드를 다시 포맷하십시오. – ggfan

답변

10

양식이 welcomedb/submit에 제출 중이지만 컨트롤러가 welcomedb/simpleform 인 것으로 보입니다. 변경해야 할 수도 있습니다.

그렇지 않으면 이상한 것 같지 않습니다. 아마

+0

그랬지만 작동하지 않습니다 – webkul

+0

마지막으로 해결책은 도움을 주셔서 감사합니다 – webkul

+3

남자에게 신용을 보여주세요 .. 그의 대답을 확인하십시오 – mosid

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가 아닙니다 !! 일반적으로 마지막 배열 항목에 쉼표를 추가하는 것이 좋습니다. –

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> 
관련 문제