douyue5856 2016-01-21 15:40
浏览 51

如何在两个不同的表中逐步保存数据?

I am trying to save data into two different tables. One is Users and another is organisation.

Now i have form where i create a organisation in that form i have some fields that i want to store in users table.

The Fields that i want to store in users table are:

first_name,last_name,email,password

and i want to store the id of this inserted record in a variable called $sales_id.

So basically firstly i want to insert the data from form into users table then when i get the $sales_id after inserting users i want to proceed with inserting the data into organization table. So there i will use sales_id variable.

the Form is :

<form role="form" action="" method="post" class="registration-form">

                <fieldset>
                    <div class="form-top">
                        <div class="form-top-left">
                            <h3>Step 1 / 3</h3>
                            <h3>Add A New Organization</h3>
                        </div>
                        <div class="form-top-right">
                            <i class="fa fa-user"></i>
                        </div>
                    </div>
                    <div class="form-bottom">
                        <div class="form-group">
                            <?php echo validation_errors(); ?>
            <?php echo form_open(); ?>
         <?php echo form_input('org_name', set_value('org_name', $company->org_name),'placeholder="Organization name..."',' class="form-first-name form-control"'); ?>

                        </div>
                        <div class="form-group">
                            <?php echo form_input('dba', set_value('dba', $company->dba),'placeholder="DBA"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('city', set_value('city', $company->city),'placeholder="City"',' class="form-first-name form-control"'); ?>
                        </div>

                         <div class="form-group">
                            <?php echo form_input('pin', set_value('pin', $company->pin),'placeholder="Pin"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('state', set_value('state', $company->state),'placeholder="State"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_textarea('addr1', set_value('addr1', $company->addr1),'placeholder="Address"',' class="form-first-name form-control"'); ?>
                        </div>
                         <div class="form-group">
                            <?php echo form_textarea('addr2', set_value('addr2', $company->addr2),'placeholder="Address 2"',' class="form-first-name form-control"'); ?>
                        </div>
                        <button type="button" class="btn btn-next">Next</button>
                    </div>
                </fieldset>




                <fieldset>
                    <div class="form-top">
                        <div class="form-top-left">
                            <h3>Step 2 / 3</h3>
                            <h3>Set up your Sales account:</h3>
                        </div>
                        <div class="form-top-right">
                            <i class="fa fa-key"></i>
                        </div>
                    </div>
                    <div class="form-bottom">
                        <div class="form-group">
                            <?php echo form_input('first_name', set_value('first_name', $user->first_name),'placeholder="First name"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('last_name', set_value('last_name', $user->last_name),'placeholder="Last Name"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('email', set_value('email', $user->email),'placeholder="Email"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_password('password', set_value('password', $user->password),'placeholder="Password"',' class="form-first-name form-control"'); ?>
                        </div>

                        <button type="button" class="btn btn-previous">Previous</button>
                        <button type="button" class="btn btn-next">Next</button>
                    </div>
                </fieldset>
                <fieldset>
                    <div class="form-top">
                        <div class="form-top-left">
                            <h3>Step 3 / 4</h3>
                            <h3>Set up your Technical account:</h3>
                        </div>
                        <div class="form-top-right">
                            <i class="fa fa-key"></i>
                        </div>
                    </div>
                    <div class="form-bottom">
                        <div class="form-group">
                            <?php echo form_input('first_name', set_value('first_name', $user->first_name),'placeholder="First name"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('last_name', set_value('last_name', $user->last_name),'placeholder="Last Name"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_input('email', set_value('email', $user->email),'placeholder="Email"',' class="form-first-name form-control"'); ?>
                        </div>

                        <div class="form-group">
                            <?php echo form_password('password', set_value('password', $user->password),'placeholder="Password"',' class="form-first-name form-control"'); ?>
                        </div>

                        <button type="button" class="btn btn-previous">Previous</button>
                        <button type="button" class="btn btn-next">Next</button>
                    </div>
                </fieldset>





                <fieldset>
                    <div class="form-top">
                        <div class="form-top-left">
                            <h3>Step 4 / 4</h3>
                            <p>Social media profiles:</p>
                        </div>
                        <div class="form-top-right">
                            <i class="fa fa-twitter"></i>
                        </div>
                    </div>
                    <div class="form-bottom">



                        <div class="form-group">
                            <?php echo form_input('tax_number', set_value('tax_number', $company->tax_number),'placeholder="Tax Number"',' class="form-first-name form-control"'); ?>
                        </div>

                          <div class="form-group">
                            <?php echo form_input('comment', set_value('comment', $company->comment),'placeholder="Comment"',' class="form-first-name form-control"'); ?>
                        </div>

                          <div class="form-group">
                            <?php echo form_input('url', set_value('url', $company->url),'placeholder="URL"',' class="form-first-name form-control"'); ?>
                        </div>

                        <button type="button" class="btn btn-previous">Previous</button>
        <?php echo form_submit('submit', 'Save', 'class="btn btn-primary"','onClick="image()"'); ?>

                <?php echo form_close();?>
                    </div>
                </fieldset>

                                    </form>

And the controller is:

public function add_company()
    {

        $this->data['company'] = $this->company_m->get_new();
        $this->data['user'] = $this->secure_m->get_new();
        //$rules = $this->company_m->rules_admin;

        //$this->form_validation->set_rules($rules);


        if ($this->form_validation->run() == TRUE)
        {

            $data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));




            $data_user = $this->user_m->array_from_post(array('first_name','last_name','email','password'));






            $this->secure_m->save($data_user,$id);
            $this->company_m->save($data, $id); 

            echo $this->db->last_query();
            //redirect('admin/company');

        }
                                            // Load the view
        $this->data['subview'] = 'admin/company/add';
        $this->load->view('admin/_layout_main', $this->data);

    }
  • 写回答

1条回答 默认 最新

  • doumi0737 2016-01-21 18:48
    关注

    How can we save data from single form in to two different tables in codeigniter, So here is a simple tutorial, i hope it will be helpful.

    Controller

    class Form extends CI_Controller {
    
        function index()
        {
            $this->load->library('form_validation');
            $config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|md5'
                  ),
               array(
                     'field'   => 'org_name',
                     'label'   => 'Organization name',
                     'rules'   => 'trim|required'
                  ),   
               array(
                     'field'   => 'org_city',
                     'label'   => 'City',
                     'rules'   => 'trim|required'
                  )
            );
    
            $this->form_validation->set_rules($config); 
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('myform');
            }
            else
            {
                $this->load->model('User_model');
                $user = new User_model();
                $user->username = $this->input->post('username');
                $user->password = $this->input->post('password');
                $user->save();
    
                $this->load->model('Organization_model');
                $organization = new Organization_model();
                $organization->user_id = $user->id;
                $organization->name    = $this->input->post('org_name');
                $organization->city    = $this->input->post('org_city');
                $organization->save();
            }
        }
    }
    

    HTML Form

    <?php echo validation_errors(); ?>
    <form action="" method="post">
        <input type="text" name="username">
        <input type="password" name="password">
    
        <input type="text" name="org_name">
        <input type="text" name="org_city">
    
        <input type="submit" name="submit" value="submit">
    </fom>
    

    User Model

    class User_model extends CI_Model {
    
        const DB_TABLE = 'user';
    
        public $id;
        public $username;
        public $password;
    
        public function save()
        {
            $data = array(
                'username' => $this->username,
                'password' => $this->password
            );
    
            $this->db->insert($this::DB_TABLE, $data);
            $this->id = $this->db->insert_id();
        }
    
    }
    

    Organization Model

    class Organization_model extends CI_Model {
    
        const DB_TABLE = 'organization';
    
        public $id;
        public $user_id;
        public $name;
        public $city;
    
        public function save()
        {
            $data = array(
                'user_id' => $this->user_id,
                'name'    => $this->name,
                'city'    => $this->city
            );
    
            $this->db->insert($this::DB_TABLE, $data);
            $this->id = $this->db->insert_id();
        }
    
    }
    
    评论

报告相同问题?