简体   繁体   中英

how can i fill the database with 2 or more table on codeigneter

i have problem, i want make a registration page, but i want fill data not just from 1 table but 2, i have the code like this :

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Pendaftaran2 extends CI_Controller {

    public function daftar()
    {
        if(isset($_POST['pendaftaran'])){
            $this->form_validation->set_rules('email','Email','required|is_unique[user.email]');
            $this->form_validation->set_rules('password','password','required|min_length[5]');
            $this->form_validation->set_rules('password2','konformasi password','required|min_length[5]|matches[password]');

            //if form validation true
            if($this->form_validation->run() == TRUE){
                echo 'form validated';
                $email = $this->input->post('email');
                $pass = md5($this->input->post('password'));


                $data = array(
                    'email' => $email,
                    'password' => $pass
                );
                $this->db->insert('user',$data);

                $this->session->set_flashdata("success","your account has been created");
                redirect('pendaftaran2/daftar','refresh');

            }
        }

        //load view
        $this->load->view('view_pendaftaran2');
        $this->load->view('footer');
    }
}

how to add the seconds table? and i want the button just 1 button to proccess the data

note : the name of second table is "personal_data" with field (name, birth and address)

You should add those fields on same form and made another array of those fields for insert. So your updated function looks like this:

public function daftar()
    {
        if(isset($_POST['pendaftaran'])){
            $this->form_validation->set_rules('email','Email','required|is_unique[user.email]');
            $this->form_validation->set_rules('password','password','required|min_length[5]');
            $this->form_validation->set_rules('password2','konformasi password','required|min_length[5]|matches[password]');

            //if form validation true
            if($this->form_validation->run() == TRUE){
                echo 'form validated';
                $email = $this->input->post('email');
                $pass = md5($this->input->post('password'));


                $data = array(
                    'email' => $email,
                    'password' => $pass
                );
                $this->db->insert('user',$data);
                $personal_data = array(
                    'name' => $this->input->post('name'),
                    'birth' => $this->input->post('birth'),
                    'address'=>$this->input->post('birth')
                );
                $this->db->insert('personal_data',$personal_data);

                $this->session->set_flashdata("success","your account has been created");
                redirect('pendaftaran2/daftar','refresh');

            }
        }

        //load view
        $this->load->view('view_pendaftaran2');
        $this->load->view('footer');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM