如何使用codeigniter将一堆会话变量加载到另一个控制器函数并将它们插入数据库中

时间:2013-01-17 09:04:50

标签: php mysql codeigniter

我正在尝试编写一个代码,其中表单将在按下提交按钮时将其所有值存储在会话变量中,并转到下一页,其中有另一个表单等待填充。当按下第2页中表单的提交按钮时,变量将存储在会话中,然后会话变量/数组的内容将插入数据库中。而我似乎遇到了问题

要点:

  1. 第1页 - 用户填写表单,变量将在提交后存储在会话数组中

  2. 第2页 - 用户填写另一个表单,变量将在提交后存储在会话数组中

  3. 第1页和第2页中的会话变量将插入数据库

  4. 这是我的代码:

    控制器(site.php)

            public function m1()
            {
                if(isset($_POST['m1']))
                {
                    $suffix = $this->input->post("suffix");
                    $fn = $this->input->post("fn");
                    $mn = $this->input->post("mn");
                    $ln = $this->input->post("ln");
                    $nickname = $this->input->post("nickname");
                    $sex = $this->input->post("sex");
                    $bday = $this->input->post("bday");
                    $street = $this->input->post("street");
                    $city = $this->input->post("city");
                    $province = $this->input->post("province");
                    $region = $this->input->post("region");
                    $landline = $this->input->post("landline");
                    $cellphone = $this->input->post("cellphone");
                    $email = $this->input->post("email");
                    $edu_level = $this->input->post("edu_level");
                    $course = $this->input->post("course");
                    $school_name = $this->input->post("school_name");
                    $school_address = $this->input->post("school_address");
                    $school_province = $this->input->post("school_province");
                    $school_city = $this->input->post("school_city");
                    $school_region = $this->input->post("school_region");
    
                    $newdata = array('suffix'=>$suffix,
                                    'fn'=>$fn,
                                    'mn'=>$mn,
                                    'ln'=>$ln,
                                    'nickname'=>$nickname,
                                    'sex'=>$sex,
                                    'bday'=>$bday,
                                    'street'=>$street,
                                    'city'=>$city,
                                    'province'=>$province,
                                    'region'=>$region,
                                    'landline'=>$landline,
                                    'cellphone'=>$cellphone,
                                    'email'=>$email,
                                    'edu_level'=>$edu_level,
                                    'course'=>$course,
                                    'school_name'=>$school_name,
                                    'school_address'=>$school_address,
                                    'school_province'=>$school_province,
                                    'school_city'=>$school_city,
                                    'school_region'=>$school_region,
                                    );
    
                    $this->session->set_userdata($newdata);
    
                    redirect(base_url() . "site/cmain/m/2");    
                }
    
            }
    
            public function m2()
            {
                if(isset($_POST['m2']))
                {
                    $nature_complaint = $this->input->post("nature_complaint");
                    $newdata = array('nature_complaint'=>$nature_complaint);
                    $this->session->set_userdata($newdata);
                    $this->db->insert('myself', $newdata); 
                    redirect(base_url() . "site/cmain/m/3");    
                }
            }
    

    我的代码的问题是第2页的会话变量(公共函数m2())是唯一插入数据库的变量。 有没有办法解决这个问题?

    感谢

2 个答案:

答案 0 :(得分:0)

您的$ newData变量将在第二个函数中重置。您已经将$ newData数组存储到会话中。在第二功能会话中重置值后,仅包含来自第二功能的数据。你需要在$ newData中添加数据而不是重置它。

答案 1 :(得分:0)

在第一个函数(M1)

以这种方式设置你的新数据

$this->session->set_userdata('newdata', $newdata);

在第二功能(M2)

    public function m2()
    {
        if(isset($_POST['m2']))
        {
            $nature_complaint = $this->input->post("nature_complaint");

            $newdata = array();
            $newdata = $this->session->userdata('newdata');
            $newdata['nature_complaint'] = $nature_complaint;

            $this->session->set_userdata('newdata', $newdata);

            $this->db->insert('myself', $newdata); 
            redirect(base_url() . "site/cmain/m/3");    
        }
    }