0

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

概括:

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

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

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

这是我的代码:

控制器(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())是唯一插入数据库的变量。有没有办法解决这个问题?

谢谢

4

2 回答 2

0

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

于 2013-01-17T09:13:45.357 回答
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");    
        }
    }
于 2013-01-17T09:56:44.897 回答