【问题标题】:Form's data can not post (Codeigniter)表单的数据无法发布(Codeigniter)
【发布时间】:2016-09-04 21:36:10
【问题描述】:

我正在使用 Codigniter,我在发布表单数据时遇到了问题.. 在我的表格中,有不同的部分,我已经为不同的部分放置了数组

这是我的代码(控制器)

        <?php
        class Resume extends CI_Controller{
        var $user_id;
        function __construct(){
        parent::__construct();
        $this->user_id = $this->session->userdata('user_id');
        $this->load->model("Resume_model");
        if($this->session->userdata('user_id') == NULL){
            redirect('user/login','refresh');
        }
    }
    public function index(){
        $this->Resume();
    }
    public function Resume(){

        if($this->input->post('Skills')){
            $Resume = $this->input->post();
            $ResumeInsert=array();
            $ResumeUpdate=array();
            $Key_Resume__ExistIDs=array();

                foreach($this->input->post('Person_id') as $key =>$value ){
                    $ResumeInsert[$key]['updated'] = Date("Y-m-d");
                    $ResumeInsert[$key]['Importance_JK'] = $Resume['Importance_JK'];
                    $ResumeInsert[$key]['user_FK'] = $user_id;
                    $ResumeInsert[$key]['Person_id'] = $Resume['Person_id'][$key];
                    $ResumeInsert[$key]['email_contact'] = $Resume['email_contact'][$key];
                    $ResumeInsert[$key]['Contact_Phone'] = $Resume['Contact_Phone'][$key];
                    $ResumeInsert[$key]['communication'] = $Resume['communication'][$key];
                    $ResumeInsert[$key]['creativity'] = $Resume['creativity'][$key];
                    $ResumeInsert[$key]['team_work'] = $Resume['team_work'][$key];
                    $ResumeInsert[$key]['organizational'] = $Resume['organizational'][$key];
                    $ResumeInsert[$key]['Leadership'] = $Resume['Leadership'][$key];
                    $ResumeInsert[$key]['Productivity'] = $Resume['Productivity'][$key];
                    $ResumeInsert[$key]['Postal_Address'] = $Resume['Postal_Address'][$key];
                    $ResumeInsert[$key]['Career_objective'] = $Resume['Career_objective'][$key];

                }

            foreach($this->input->post('emp_id') as $key =>$value ){
                    $ResumeInsert[$key]['resume_keyid'] = $Resume['emp_id'][$key];
                    $ResumeInsert[$key]['employer_name'] = $Resume['employer_name'][$key];
                    $ResumeInsert[$key]['start_Date'] = $Resume['start_Date'][$key];
                    $ResumeInsert[$key]['end_date'] = $Resume['end_date'][$key];
                    $ResumeInsert[$key]['type_id'] = $Resume['type_id'][$key];
                    $ResumeInsert[$key]['position'] = $Resume['position'][$key];
                    $ResumeInsert[$key]['responsibility'] = $Resume['responsibility'][$key];
                    $ResumeInsert[$key]['Skills'] = $Resume['Skills'][$key];
                    print_r ($ResumeInsert);exit;
            }

            foreach($this->input->post('resume_id1') as $key =>$value ){
                    $ResumeInsert[$key]['edu_id'] = $Resume['resume_id1'][$key];
                    $ResumeInsert[$key]['year_gain'] = $Resume['year_gain'][$key];
                    $ResumeInsert[$key]['qualification_name'] = $Resume['qualification_name'][$key];
                    $ResumeInsert[$key]['institution_name'] = $Resume['institution_name'][$key];
                    $ResumeInsert[$key]['outstanding_Ach'] = $Resume['outstanding_Ach'][$key];
                    $ResumeInsert[$key]['aquired_skill'] = $Resume['aquired_skill'][$key];
                    $ResumeInsert[$key]['expiry'] = $Resume['expiry'][$key];
                    $ResumeInsert[$key]['Application_of_Skill'] = $Resume['Application_of_Skill'][$key];

                }
            foreach($this->input->post('resume_id2') as $key =>$value ){
                    $ResumeInsert[$key]['resume_skillID'] = $Resume['resume_id2'][$key];
                    $ResumeInsert[$key]['skill_name'] = $Resume['skill_name'][$key];
                    $ResumeInsert[$key]['Licences_permits'] = $Resume['Licences_permits'][$key];
                    $ResumeInsert[$key]['date_Achived'] = $Resume['date_Achived'][$key];
                    $ResumeInsert[$key]['expiry_renewal'] = $Resume['expiry'][$key];
                    $ResumeInsert[$key]['skill_work'] = $Resume['application_of_skill'][$key];
                    $ResumeInsert[$key]['course_tilte'] = $Resume['course_tilte'][$key];

                }

            foreach($this->input->post('resume_id3') as $key =>$value ){

                    $ResumeInsert[$key]['hobbyId'] = $Resume['resume_id3'][$key];
                    $ResumeInsert[$key]['hobby_name'] = $Resume['hobby_name'][$key];
                    $ResumeInsert[$key]['achievement_name'] = $Resume['achievement_name'][$key];
                    $ResumeInsert[$key]['Application_OF_hobby_work'] = $Resume['Application_of_Skill'][$key];

                }

                foreach($this->input->post('resume_id4') as $key =>$value ){

                    $ResumeInsert[$key]['ref_resume_ID'] = $Resume['resume_id4'][$key];
                    $ResumeInsert[$key]['referee_name'] = $Resume['referee_name'][$key];
                    $ResumeInsert[$key]['Position_referee'] = $Resume['Position'][$key];
                    $ResumeInsert[$key]['company_name_ref'] = $Resume['company_name'][$key];
                    $ResumeInsert[$key]['phone_ref'] = $Resume['phone_ref'][$key];
                    $ResumeInsert[$key]['contact_email_ref'] = $Resume['contact_email'][$key];
                    $ResumeInsert[$key]['reference_Type'] = $Resume['reference_Type'][$key];
                    $ResumeInsert[$key]['Application_of_Skill_ref'] = $Resume['Application_of_Skill'][$key];

                }

                foreach($this->input->post('resume_id5') as $key =>$value ){

                    $ResumeInsert[$key]['Application_Id'] = $Resume['resume_id5'][$key];
                    $ResumeInsert[$key]['app_date_sent'] = $Resume['date_sent'][$key];
                    $ResumeInsert[$key]['app_busisness_name'] = $Resume['app_busisness_name'][$key];
                    $ResumeInsert[$key]['app_address'] = $Resume['app_address'][$key];
                    $ResumeInsert[$key]['app_contact_phone'] = $Resume['app_contact_phone'][$key];
                    $ResumeInsert[$key]['app_outcome'] = $Resume['app_outcome'][$key];
                    $ResumeInsert[$key]['app_date_closed'] = $Resume['app_date_closed'][$key];

                }
                       if(isset($Resume['id'][$key]))
                       {
                        // echo "test";exit;
                        $Key_Resume__ExistIDs[]=$Resume['id'][$key];
                        if( $ResumeInsert[$key]['Person_id'] != ""){
                            $ResumeUpdate[$key]['Person_id'] = $ResumeInsert[$key]['Person_id'];
                        }
                           if( $ResumeInsert[$key]['email_contact'] != ""){
                            $ResumeUpdate[$key]['email_contact'] = $ResumeInsert[$key]['email_contact'];
                        }
                           if( $ResumeInsert[$key]['Contact_Phone'] != ""){
                            $ResumeUpdate[$key]['Contact_Phone'] = $ResumeInsert[$key]['Contact_Phone'];
                        }
                           if( $ResumeInsert[$key]['communication'] != ""){
                            $ResumeUpdate[$key]['communication'] = $ResumeInsert[$key]['communication'];
                        }
                           if( $ResumeInsert[$key]['creativity'] != ""){
                            $ResumeUpdate[$key]['creativity'] = $ResumeInsert[$key]['creativity'];
                        }
                           if( $ResumeInsert[$key]['team_work'] != ""){
                            $ResumeUpdate[$key]['team_work'] = $ResumeInsert[$key]['team_work'];
                        }
                           if( $ResumeInsert[$key]['organizational'] != ""){
                            $ResumeUpdate[$key]['organizational'] = $ResumeInsert[$key]['organizational'];
                        }
                           if( $ResumeInsert[$key]['Leadership'] != ""){
                            $ResumeUpdate[$key]['Leadership'] = $ResumeInsert[$key]['Leadership'];
                        }
                           if( $ResumeInsert[$key]['Productivity'] != ""){
                            $ResumeUpdate[$key]['Productivity'] = $ResumeInsert[$key]['Productivity'];
                        }
                           if( $ResumeInsert[$key]['Postal_Address'] != ""){
                            $ResumeUpdate[$key]['Postal_Address'] = $ResumeInsert[$key]['Postal_Address'];
                        }

                           if( $ResumeInsert[$key]['Career_objective'] != ""){
                            $ResumeUpdate[$key]['Career_objective'] = $ResumeInsert[$key]['Career_objective'];
                        }


                        if( $ResumeInsert[$key]['resume_keyid'] != ""){
                            $ResumeUpdate[$key]['resume_keyid'] = $ResumeInsert[$key]['resume_keyid'];
                        }
                        if( $ResumeInsert[$key]['employer_name'] != ""){
                            $ResumeUpdate[$key]['employer_name'] = $ResumeInsert[$key]['employer_name'];
                        }
                        if( $ResumeInsert[$key]['start_Date'] != ""){
                            $ResumeUpdate[$key]['start_Date'] = $ResumeInsert[$key]['start_Date'];
                        }
                        if( $ResumeInsert[$key]['end_date'] != "") {
                            $ResumeUpdate[$key]['end_date'] = $ResumeInsert[$key]['end_date'];
                       }
                        if( $ResumeInsert[$key]['type_id'] != ""){
                            $ResumeUpdate[$key]['type_id'] = $ResumeInsert[$key]['type_id'];
                      }
                        if( $ResumeInsert[$key]['position'] != ""){
                            $ResumeUpdate[$key]['position'] = $ResumeInsert[$key]['position'];
                     }

                        if( $ResumeInsert[$key]['edu_id'] != ""){
                            $ResumeUpdate[$key]['edu_id'] = $ResumeInsert[$key]['edu_id'];
                        }
                        if( $ResumeInsert[$key]['year_gain'] != ""){
                            $ResumeUpdate[$key]['year_gain'] = $ResumeInsert[$key]['year_gain'];
                        }
                        if( $ResumeInsert[$key]['qualification_name'] != ""){
                            $ResumeUpdate[$key]['qualification_name'] = $ResumeInsert[$key]['qualification_name'];
                        }
                        if( $ResumeInsert[$key]['institution_name'] != ""){
                            $ResumeUpdate[$key]['institution_name'] = $ResumeInsert[$key]['institution_name'];
                        }
                        if( $ResumeInsert[$key]['outstanding_Ach'] != ""){
                            $ResumeUpdate[$key]['outstanding_Ach']= $ResumeInsert[$key]['outstanding_Ach'];
                        }
                        if( $ResumeInsert[$key]['aquired_skill'] != ""){
                            $ResumeUpdate[$key]['aquired_skill'] = $ResumeInsert[$key]['aquired_skill'];
                        }
                        if( $ResumeInsert[$key]['expiry'] != ""){
                            $ResumeUpdate[$key]['expiry'] = $ResumeInsert[$key]['expiry'];
                        }
                        if( $ResumeInsert[$key]['Application_of_Skill'] != ""){
                            $ResumeUpdate[$key]['Application_of_Skill'] = $ResumeInsert[$key]['Application_of_Skill'];
                        }
                        if( $ResumeInsert[$key]['resume_skillID'] != ""){
                            $ResumeUpdate[$key]['resume_skillID'] = $ResumeInsert[$key]['resume_skillID'];
                        }
                        if( $ResumeInsert[$key]['skill_name'] != ""){
                            $ResumeUpdate[$key]['skill_name'] = $ResumeInsert[$key]['skill_name'];
                        }
                        if( $ResumeInsert[$key]['Licences_permits'] != ""){
                            $ResumeUpdate[$key]['Licences_permits'] = $ResumeInsert[$key]['Licences_permits'];
                        }
                        if( $ResumeInsert[$key]['date_Achived'] != ""){
                            $ResumeUpdate[$key]['date_Achived'] = $ResumeInsert[$key]['date_Achived'];
                        }
                        if( $ResumeInsert[$key]['expiry_renewal'] !="") {
                            $ResumeUpdate[$key]['expiry_renewal'] = $ResumeInsert[$key]['expiry_renewal'];
                        }
                        if( $ResumeInsert[$key]['skill_work'] != ""){
                            $ResumeUpdate[$key]['skill_work'] = $ResumeInsert[$key]['skill_work'];
                        }
                        if( $ResumeInsert[$key]['course_tilte'] != ""){
                            $ResumeUpdate[$key]['course_tilte'] = $ResumeInsert[$key]['course_tilte'];
                        }
                        if( $ResumeInsert[$key]['hobbyId'] != ""){
                            $ResumeUpdate[$key]['hobbyId'] = $ResumeInsert[$key]['hobbyId'];
                        }
                        if( $ResumeInsert[$key]['hobby_name'] != ""){
                            $ResumeUpdate[$key]['hobby_name'] = $ResumeInsert[$key]['hobby_name'];
                        }
                        if( $ResumeInsert[$key]['achievement_name'] != ""){
                            $ResumeUpdate[$key]['achievement_name'] = $ResumeInsert[$key]['achievement_name'];
                        }
                        if( $ResumeInsert[$key]['Application_OF_hobby_work'] != ""){
                            $ResumeUpdate[$key]['Application_OF_hobby_work'] = $ResumeInsert[$key]['Application_OF_hobby_work'];
                        }
                        if( $ResumeInsert[$key]['ref_resume_ID'] != ""){
                            $ResumeUpdate[$key]['ref_resume_ID'] = $ResumeInsert[$key]['ref_resume_ID'];
                        }
                        if( $ResumeInsert[$key]['referee_name'] != ""){
                            $ResumeUpdate[$key]['referee_name'] = $ResumeInsert[$key]['referee_name'];
                        }
                        if( $ResumeInsert[$key]['Position_referee'] != ""){
                            $ResumeUpdate[$key]['Position_referee'] = $ResumeInsert[$key]['Position_referee'];
                        }
                        if( $ResumeInsert[$key]['company_name_ref'] != ""){
                            $ResumeUpdate[$key]['company_name_ref'] = $ResumeInsert[$key]['company_name_ref'];
                        }
                        if( $ResumeInsert[$key]['phone_ref'] != ""){
                            $ResumeUpdate[$key]['phone_ref'] = $ResumeInsert[$key]['phone_ref'];
                        }
                        if( $ResumeInsert[$key]['contact_email_ref'] != ""){
                            $ResumeUpdate[$key]['contact_email_ref'] = $ResumeInsert[$key]['contact_email_ref'];
                        }
                        if( $ResumeInsert[$key]['reference_Type'] != ""){
                            $ResumeUpdate[$key]['reference_Type'] = $ResumeInsert[$key]['reference_Type'];
                        }
                        if( $ResumeInsert[$key]['Application_of_Skill_ref'] != ""){
                            $ResumeUpdate[$key]['Application_of_Skill_ref'] = $ResumeInsert[$key]['Application_of_Skill_ref'];
                        }
                        if( $ResumeInsert[$key]['app_date_sent'] != ""){
                            $ResumeUpdate[$key]['app_date_sent'] = $ResumeInsert[$key]['app_date_sent'];
                        }
                         if( $ResumeInsert[$key]['Application_Id'] != ""){
                            $ResumeUpdate[$key]['Application_Id'] = $ResumeInsert[$key]['Application_Id'];
                         }
                         if( $ResumeInsert[$key]['app_busisness_name'] != ""){
                            $ResumeUpdate[$key]['app_busisness_name'] = $ResumeInsert[$key]['app_busisness_name'];
                         }
                         if( $ResumeInsert[$key]['app_address'] != ""){
                            $ResumeUpdate[$key]['app_address'] = $ResumeInsert[$key]['app_address'];
                         }
                         if( $ResumeInsert[$key]['app_contact_phone'] != "")
                           {
                            $ResumeUpdate[$key]['app_contact_phone'] = $ResumeInsert[$key]['app_contact_phone'];
                           }
                         if( $ResumeInsert[$key]['app_outcome'] != ""){
                            $ResumeUpdate[$key]['app_outcome'] = $ResumeInsert[$key]['app_outcome'];
                         }
                         if( $ResumeInsert[$key]['app_date_closed'] != ""){
                            $ResumeUpdate[$key]['app_date_closed'] = $ResumeInsert[$key]['app_date_closed'];
                         }
                         if( $ResumeInsert[$key]['resume_id'] != ""){
                            $ResumeUpdate[$key]['resume_id'] = $ResumeInsert[$key]['resume_id'];
                         }

                         $ResumeUpdate[$key]['resume_id']=$Resume['id'][$key];
                        unset($ResumeInsert[$key]);
                      }
                     else{
                        $ResumeInsert[$key]['resume_id'] = $GetLastID;
                        $GetLastID++;
                    }

                $status=$this->Resume_model->ProcessData($idsToDelete,$ResumeUpdate,$user_id,$ResumeInsert,$imgInsert,$imgUpdate);
                redirect('Resume','refresh');   

        }
    }
?>

此处插入查询(模型)

function ProcessData($tbl_resumeInsert){

      if(!empty($tbl_resumeInsert))

     {

             $this->insert_tbl_resume($user_id,$tbl_resumeInsert);
     }

}

function insert_tbl_resume($id,$arrtbl_resume)

{

       $this->db->insert_batch('tbl_resume', $arrtbl_resume); 

}

在上面的代码中,当我要提交表单时,只有第一个Foreach loop的数据可以插入,当我打印$ResumeInsert时,它只显示我第一个 foreach 循环的数据foreach($this-&gt;input-&gt;post('Person_id') as $key =&gt;$value ) Rest foreach 循环的数据出现在 DB

NULL

请帮忙? 谢谢!

【问题讨论】:

  • 你的html也可以放上去

标签: php mysql forms codeigniter


【解决方案1】:

函数$this-&gt;input-&gt;post('something') 正在从$_POST 数组中返回一个值,其键为“某物”。如果键 'something' 不存在,则返回 NULL。 (或在 CI 版本

$_POST 是一个关联数组,最多只有一个具有任何给定键的项目。所以代码

foreach($this->input->post('Person_id') as $key =>$value )
{
  ...

将只运行一次,因为只有一个 $_POST 数组项具有键“Person_id”。

如果您的表单 html 有多个名为“Person_id”的字段,则只有最后一个字段的值将被发布到服务器。

【讨论】:

  • 我的 HTML 中只有一个名为“Person_id”的字段。如果我在第二个 foreach 循环中打印类似 echo "xyz" 的内容(foreach($this-> input->post('emp_id') as $key =>$value ){) 然后我可以在浏览器中获取“Xyz”,如果我打印 $Resume['employer_name'][$ key];,那么我可以得到 "Employer name" 的值,但是如果我打印 ($ResumeInsert) ,那么我只得到第一个 foreach 循环的数据
  • foreach($this-&gt;input-&gt;post('emp_id') as $key =&gt;$value ) 正在循环通过 $_POST 数组。你只得到第一个,因为$_POST 中只有一个项目,键为“emp_id”。如果存储在$this-&gt;input-&gt;post('emp_id') 的值的数据类型 是一个数组,并且您想从该数组 中提取值,那么您应该这样做$data = $this-&gt;input-&gt;post('emp_id'); foreach($data as $key =&gt; $value){... 因为您已经在$Resume 中捕获整个$_POST 数组,您可以(应该)简单地使用foreach($Resume['emp_id'] as $key =&gt; $value){...
  • 如果您将问题编辑为包含&lt;form&gt; 的html 或输入结构的解释,那么提供建议会容易得多。
猜你喜欢
  • 1970-01-01
  • 2021-06-26
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 2018-11-02
  • 1970-01-01
相关资源
最近更新 更多