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

时间:2016-01-21 15:40:31

标签: php codeigniter

我正在尝试将数据保存到两个不同的表中。一个是用户,另一个是组织。

现在我有一个表格,我以这种形式创建一个组织,我有一些字段,我想存储在用户表中。

我想要存储在users表中的字段是:

  

名字,姓氏,电子邮件,密码

我希望将此插入记录的id存储在名为$ sales_id的变量中。

所以基本上我首先要将表格中的数据插入到用户表中,然后当我在插入用户后获得$ sales_id时,我想继续将数据插入到组织表中。所以我将使用sales_id变量。

表格是:

<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>

控制器是:

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 个答案:

答案 0 :(得分:1)

我们如何将单个表单中的数据保存到codeigniter中的两个不同表中,所以这是一个简单的教程,我希望它会有所帮助。

<强>控制器

$("#previous").on('click', function(){
    $("#2DivT").effect( "slide", { "direction" : "left",  "mode" : "hide"}, 900);
    $("#1DivT").effect( "slide", { "direction" : "right",  "mode" : "show"}, 900);
});

$("#next").on('click', function(){
    $("#1DivT").effect( "slide", { "direction" : "right",  "mode" : "hide"}, 900);
    $("#2DivT").effect( "slide", { "direction" : "left",  "mode" : "show"}, 900);
});

HTML表单

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

用户模型

<?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>

组织模式

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

}