如何用codeigneter上的2个或更多表填充数据库

时间:2017-03-18 06:41:21

标签: php mysql database codeigniter

  

我有问题,我想要一个注册页面,但我想要填充数据不只是从1个表而是2,我有这样的代码:

<?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');
    }
}
  

如何添加秒表?我希望按钮只需1个按钮来处理数据

     

注意:第二个表的名称是“personal_data”,其中包含字段(姓名,出生地址和地址)

1 个答案:

答案 0 :(得分:0)

您应该在同一表单上添加这些字段,并为插入创建另一个字段数组。 所以你更新的函数如下所示:

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