What is the best and fast way to insert data in mu

2019-03-04 16:30发布

问题:

I am using Codeginator, I have three table (It will increase in the future according to requirement) and tables name are "tbl_customer", "tbl_customer_billing","tbl_customer_shipping". Each table is connected with primary key and foreign key.

Now I am inserting the data in each table so I tried below code.

My question is:

1) Should I use $this->security->xss_clean() for each array? or any other idea to use only a single xss_clean?

2) What is the best and fast way to insert the data in the multiple tables?

I tried below code. Is it fine or should I use something another way? I tried below code but there are lots of changes to lose the data. Is there any other idea?

Controller:

public function add_newcustomer(){
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('c_firstname', 'Firstname', 'trim|required|min_length[3]|max_length[25]');
        $this->form_validation->set_rules('c_lastname', 'Lastname', 'trim|required|min_length[3]|max_length[25]');

        $this->form_validation->set_rules('c_email', 'Email', 'required');
        $this->form_validation->set_rules('c_mobileno', 'Mobile no', 'trim|required|regex_match[/^[0-9]{10}$/]');
        $this->form_validation->set_rules('c_billing_address', 'Billing Address', 'required|min_length[10]|max_length[100]');
        $this->form_validation->set_rules('c_b_country', 'Country', 'required');
        $this->form_validation->set_rules('c_b_state', 'State', 'required');
        $this->form_validation->set_rules('c_b_city', 'City', 'required');
        $this->form_validation->set_rules('c_b_zipcode', 'Zip code', 'required');
        $this->form_validation->set_rules('c_shopping_address', 'Shopping Address', 'required|min_length[10]|max_length[100]');
        $this->form_validation->set_rules('c_s_country', 'Country', 'required');
        $this->form_validation->set_rules('c_s_state', 'State', 'required');
        $this->form_validation->set_rules('c_s_city', 'City', 'required');
        $this->form_validation->set_rules('c_s_zipcode', 'Zip code', 'required');
        if ($this->form_validation->run() == FALSE)
                {
                $data['get_country']=$this->Customer_model->get_country();// all country name
                $this->load->view('create_order',$data);
                 }
                 else
                {
        $cust_personal = array(
                'c_firstname'=>$this->input->post('c_firstname'),
                'c_middlename'=>$this->input->post('c_middlename'),
                'c_lastname'=>$this->input->post('c_lastname'),
                'c_email_id'=>$this->input->post('c_email'),
                'c_mobileno'=>$this->input->post('c_mobileno'),
                'c_alternetno'=>$this->input->post('c_alternetno'),
                'c_created_by_emp'=>$this->session->userdata['login_session']['id'],
                'c_date_of_added'=>$this->current_date
                 );
            $this->db->insert('tbl_customer',$cust_personal);
            $last_cust_id= $this->db->insert_id();


        $cust_billing = array(
                'c_b_address'=>$this->input->post('c_billing_address'),
                'c_b_country'=>$this->input->post('c_b_country'),
                'c_b_state'=>$this->input->post('c_b_state'),
                'c_b_city'=>$this->input->post('c_b_city'),
                'c_b_zipcode'=>$this->input->post('c_b_zipcode'),
                'cust_id'=>$last_cust_id,
                'c_date_of_added'=>$this->current_date
            );
            $this->db->insert('tbl_customer_billing',$cust_billing);
            $last_billing_id= $this->db->insert_id();

         $cust_shipping = array(

                'c_s_address '=>$this->input->post('c_shopping_address'),
                'c_s_country'=>$this->input->post('c_s_country'),
                'c_s_state'=>$this->input->post('c_s_state'),
                'c_s_city'=>$this->input->post('c_s_city'),
                'c_s_zipcode'=>$this->input->post('c_s_zipcode'),
                'c_s_receiver_no'=>$this->input->post('c_receiver_no'),
                'cust_id'=>$last_cust_id,
                'c_billing_id'=>$last_billing_id,
                'c_date_of_added'=>$this->current_date
            );

            $this->db->insert('tbl_customer_shipping',$cust_shipping);

            redirect("Customer_control/index");

                }
}