使用mvc框架使用面向对象的程序插入数据

时间:2017-01-16 17:07:26

标签: codeigniter

我正在尝试在数据库中添加学生,但学生没有被添加到数据库中。

控制器的代码如下:

public function add_student(){
    $this->load->model('Stud_Model');
    $data=array(
    'roll_no'=>$this->input->post('roll_no'),
    'name'=>$this->input->post('name'),
    'role'=>$this->input->post('role'),
    );
    $this->Stud_Model->insert($data);

    $query=$this->db->get("stud");
    $data['records']=$query->result();
    $this->load->view('Stud_view',$data);
}

模型的代码如下:

public function insert($data){
    $this->db->insert('stud',$data);
    if($this->db->insert('stud',$data)){
        return true;
    }
}

视图的代码如下:

<?php
    echo form_open('Stud_controller/add_student');
    echo form_label('Roll No.:');
    echo form_input(array('id'=>'roll_no','name'=>'roll_no','role'=>'roll_no'));
    echo "<br/>";
    echo form_label('name:');
    echo form_input(array('id'=>'name','name'=>'name','role'=>'name'));
    echo "<br/>";
    echo form_label('Role:');
    echo form_input(array('id'=>'roll_no','name'=>'roll_no','role'=>'role'));
    echo "<br/>";
    echo form_submit(array('id'=>'submit','value'=>'Add'));
    echo form_close();
?>

1 个答案:

答案 0 :(得分:0)

        for model 
         public function insert($data)
           {
            return $this->db->insert('stud',$data);   
           }

        for retrive data in model

          public function GetAllStudent()
             {
                $this->db->select('*');
                $this->db->from('stud');
                $query = $this->db->get();
                return $query->result();
             }
    for retrive data in controller 
    $data['all_std_info'] = $this->Stud_Model->GetAllStudent();