使用codeigniter在数据库中添加数据

时间:2014-09-18 07:04:19

标签: html database codeigniter

我无法将数据添加到数据库这里使用codeigniter代码,我也可以将所有代码相互连接到Controller,Model,View和数据库名称:corporate **和table name of我的数据库是dept_add

 <?php

    class Member extends CI_Controller {

    function index()
    {

        $this->load->library('form_validation');

           $this->form_validation->set_rules('type_one', 'Option',     'trim|required|xss_clean');
            $this->form_validation->set_rules('user_id', 'User Id', 'trim|required|xss_clean');
            $this->form_validation->set_rules('dept_id', 'Department Id', 'trim|required|xss_clean');
            $this->form_validation->set_rules('credit_limit', 'Credit Limit', 'trim|required|xss_clean');
            $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
            $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('type_two', 'Address', 'trim|required|xss_clean');
            $this->form_validation->set_rules('rec_first_name', 'Reciepient First Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('rec_last_name', 'Reciepient Last Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('phone_no', 'Phone Number', 'trim|required|xss_clean');
            $this->form_validation->set_rules('house_no', 'House Number', 'trim|required|xss_clean');
            $this->form_validation->set_rules('block_no', 'Block Number', 'trim|required|xss_clean');
            $this->form_validation->set_rules('lot_no', 'Lot Number', 'trim|required|xss_clean');
            $this->form_validation->set_rules('street_no', 'Street No and Street Name ', 'trim|required|xss_clean');
            $this->form_validation->set_rules('barangay', 'Barangay/Village/Subdivision', 'trim|required|xss_clean');
            $this->form_validation->set_rules('landmark', 'Land Mark', 'trim|required|xss_clean');          
            $this->form_validation->set_rules('area', 'Area', 'trim|required|xss_clean');


        if ($this->form_validation->run() == FALSE)
        {
           $this->load->view('department/addnewform_edit_v');
        }
        else
        {
            $this->member_m->save($data);
        }
    }


}
?>

模型:

  <?php

class member_m extends CI_Model {

    protected $tbl_dept_add = 'dept_add';

    function __construct()
    {
        //Call the Model constructor

        parent::__construct();

    }

    function add_member()
    {

        $type=$this->input->get_post('type_one');
        $type=$this->input->get_post('user_id');
        $type=$this->input->get_post('dept_id');
        $type=$this->input->get_post('credit_limit');
        $type=$this->input->get_post('user_name');
        $type=$this->input->get_post('password');
        $type=$this->input->get_post('first_name');
        $type=$this->input->get_post('last_name');
        $type=$this->input->get_post('type_two');
        $type=$this->input->get_post('rec_first_name');
        $type=$this->input->get_post('rec_last_name');
        $type=$this->input->get_post('phone_no');
        $type=$this->input->get_post('house_no');
        $type=$this->input->get_post('block_no');
        $type=$this->input->get_post('lot_no');
        $type=$this->input->get_post('street_no');
        $type=$this->input->get_post('barangay');
        $type=$this->input->get_post('landmark');
        $type=$this->input->get_post('area');

        $query =  $this->db->query("    INSERT INTO  ".$this->tbl_dept_add."
                                                        ( 
                                                            type_one,
                                                            user_id,
                                                            dept_id,
                                                            credit_limit,
                                                            user_name,
                                                            password,
                                                            first_name,
                                                            last_name,
                                                            type_two,
                                                            rec_first_name,
                                                            rec_last_name,
                                                            phone_no,
                                                            house_no,
                                                            block_no,
                                                            lot_no,
                                                            street_no,
                                                            barangay,
                                                            landmark,
                                                            area
                                                        )
                                                        VALUES
                                                        (
                                                            '$type_one',
                                                            '$user_id,
                                                            '$dept_id',
                                                            '$credit_limit',
                                                            '$user_name',
                                                            '$password',
                                                            '$first_name',
                                                            '$last_name',
                                                            '$type_two',
                                                            '$rec_first_name',
                                                            '$rec_last_name',
                                                            '$phone_no',
                                                            '$house_no',
                                                            '$block_no',
                                                            '$lot_no',
                                                            '$street_no',
                                                            '$barangay',
                                                            '$landmark',
                                                            '$area'
                                                        )");
        public function save($data) {
        $this->db->set($data);
        $this->db->insert(dept_add);
        $this->db->insert_id();
 }
    }


}
?>

1 个答案:

答案 0 :(得分:0)

function add_member() {

$data = $this->input->post(NULL, TRUE);

$query =  $this->db->insert($tbl_dept_add, $data)
return $this->db->insert_id();

}

OR

function add_member() {

$data =array(
    'type_one' => $this->input->post('type_one'),
    'user_id' => $this->input->post('user_id'),
    'dept_id' => $this->input->post('dept_id'),

    'table_column' => 'value',
    so on...

);

$query =  $this->db->insert($tbl_dept_add, $data)
return $this->db->insert_id();

}