使用codeigniter插入数据库

时间:2010-06-23 14:01:41

标签: codeigniter

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(); ?>

感谢您的帮助

3 个答案:

答案 0 :(得分:10)

您的表单已提交至welcomedb/submit,但您的控制器似乎位于welcomedb/simpleform ...也许您需要更改该表单。

否则,似乎没有任何错误。

答案 1 :(得分: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')
);

并始终转储错误。

答案 2 :(得分: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>