【问题标题】:add data in database using codeigniter使用 codeigniter 在数据库中添加数据
【发布时间】:2014-09-18 07:04:19
【问题描述】:

我无法在此处将数据添加到数据库中,我使用 codeigniter 作为代码,而且我的所有代码都可以相互连接控制器、模型、视图以及数据库名称:公司**和我的数据库的表名是 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();
 }
    }


}
?>

【问题讨论】:

    标签: html database codeigniter


    【解决方案1】:
    function add_member() {
    
    $data = $this->input->post(NULL, TRUE);
    
    $query =  $this->db->insert($tbl_dept_add, $data)
    return $this->db->insert_id();
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多