php - 在 CodeIgniter 中内爆 Select2 表单中的多个值并插入到数据库

标签 php mysql codeigniter jquery-select2

我正在使用 select2 下拉列表中的多个选择,并且我想将从表单中选择的所有多个值存储在

数据库“xyz”

表名='pp_companies'

row="category1" <-- 需要此行是一个数组,其中包含 CSV 格式的选定值)

查看文件

<div class="titlehead">Courses Offered</div>
    <div class="input-group <?php echo (form_error('category1'))?'has-error':'';?>">
    <label class="input-group-addon">Category </label>
    <select name="category1" class="js-example-basic-multiple form-control" multiple="multiple">
      <option value="Engineering" <?php echo (set_value('category1')=='Engineering')?'selected':''; ?>>Engineering</option>
      <option value="Management" <?php echo (set_value('category1')=='Management')?'selected':''; ?>>Management</option>
      <option value="Law" <?php echo (set_value('category1')=='Law')?'selected':''; ?>>Law</option>
    </select>

一旦我提交表单,它仅发送从输入中选择的最后一个数据,在本例中为“法律”

我的 Controller

    public function index()
{
    $data['ads_row'] = $this->ads;
    $data['title'] = 'Create New Employer Account at '.SITE_URL;
    $data['msg']='';
    $data['result_cities'] = $this->cities_model->get_all_cities();
    $data['result_countries'] = $this->countries_model->get_all_countries();
    $data['result_industries'] = $this->industries_model->get_all_industries();
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[pp_employers.email]|strip_all_tags');  
    $this->form_validation->set_rules('pass', 'Password', 'trim|required|min_length[6]|strip_all_tags');
    $this->form_validation->set_rules('confirm_pass', 'Confirm password', 'trim|required|matches[pass]|strip_all_tags');
    $this->form_validation->set_rules('full_name', 'Your name', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('country', 'Country', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('company_city', 'City', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('mobile_phone', 'Mobile', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('company_name', 'Company name', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('industry_id', 'Industry', 'trim|strip_all_tags');
    $this->form_validation->set_rules('company_location', 'Company address', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('company_description', 'Company Description', 'trim|required|strip_all_tags|secure');
    $this->form_validation->set_rules('company_join', 'Company Join', 'trim|strip_all_tags|secure');
    $this->form_validation->set_rules('company_phone', 'Company Phone', 'trim|required|strip_all_tags');        
    $this->form_validation->set_rules('no_of_employees', 'No of Employees', 'trim|required|strip_all_tags');
    $this->form_validation->set_rules('company_website', 'Company Website', 'trim|required|strip_all_tags');        
    $this->form_validation->set_rules('category1', 'Category1', 'trim|required|strip_all_tags');    
    $this->form_validation->set_rules('captcha', 'Verification code', 'trim|required|validate_ml_spam');

    if (empty($_FILES['company_logo']['name']))
        $this->form_validation->set_rules('company_logo', 'Company Logo', 'required');

    $this->form_validation->set_error_delimiters('<div class="errowbox"><div class="erormsg">', '</div></div>');
    if ($this->form_validation->run() === FALSE) {
        $data['cpt_code'] = create_ml_captcha();

        $this->load->view('employer_signup_view',$data);
        return;

    }

    $current_date_time = date("Y-m-d H:i:s");
    $company_slug = make_slug($this->input->post('company_name'));
    $is_slug = $this->companies_model->check_slug($company_slug);
    if($is_slug>0){
        $company_slug.='-'.time();
    }
    $employer_array = array(
                            'first_name' => $this->input->post('full_name'),
                            'email' => $this->input->post('email'),
                            'pass_code' => $this->input->post('pass'),
                            'mobile_phone' => $this->input->post('mobile_phone'),
                            'home_phone' => $this->input->post('home_phone'),
                            'country' => $this->input->post('country'),
                            'city' => $this->input->post('city'),
                            'ip_address' => $this->input->ip_address(),
                            'dated' => $current_date_time
    );


    $company_array = array(
                            'company_name' => $this->input->post('company_name'),
                            'industry_ID' => $this->input->post('industry_id'),
                            'company_phone' => $this->input->post('company_phone'),
                            'company_location' => $this->input->post('company_location'),
                            'company_city' => $this->input->post('company_city'),
                            'company_website' => $this->input->post('company_website'),
                            'no_of_employees' => $this->input->post('no_of_employees'),                             
                            'category1'=> $this->input->post('category1'),                              
                            'company_description' => $this->input->post('company_description'),
                            'company_join' => $this->input->post('company_join'),
                            'company_slug' => $company_slug,
                            'ownership_type' => $this->input->post('ownership_type')
    );
    if (!empty($_FILES['company_logo']['name'])){


        $company_name_for_file = strtolower($this->input->post('company_name'));
        $real_path = realpath(APPPATH . '../public/uploads/employer/');
        $config['upload_path'] = $real_path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['overwrite'] = true;
        $config['max_size'] = 6000;
        $config['file_name'] = 'JOBPORTAL-'.time();
        $this->upload->initialize($config);
        if ($this->upload->do_upload('company_logo')){
            /*if($obj_row->company_logo){
                @unlink($real_path.'/'.$obj_row->company_logo); 
                @unlink($real_path.'/thumb/'.$obj_row->company_logo);
            }*/
        }

        $image = array('upload_data' => $this->upload->data()); 
        $image_name = $image['upload_data']['file_name'];
        $company_array['company_logo']=$image_name;
        $thumb_config['image_library'] = 'gd2';
        $thumb_config['source_image']   = $real_path.'/'.$image_name;
        $thumb_config['new_image']  = $real_path.'/thumb/'.$image_name;
        $thumb_config['maintain_ratio'] = TRUE;
        $thumb_config['height'] = 50;
        $thumb_config['width']   = 70;

        $this->image_lib->initialize($thumb_config);
        $this->image_lib->resize();
    }
    $company_id = $this->companies_model->add_company($company_array);
    $employer_array['company_ID'] = $company_id;
    $employer_id = $this->employers_model->add_employer($employer_array);

    $user_data = array(
            'user_id' => $employer_id,
             'user_email' => $this->input->post('email'),
             'first_name' => $this->input->post('full_name'),
             'slug' => $company_slug,
             'last_name' => '',
             'is_user_login' => TRUE,
             'is_job_seeker' => FALSE,
             'is_employer' => TRUE
             );
    $this->session->set_userdata($user_data);

    //Sending email to the user
    $row_email = $this->email_model->get_records_by_id(3);
    $config = array();
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';

    $this->email->initialize($config);
    $this->email->clear(TRUE);
    $this->email->from($row_email->from_email, $row_email->from_name);
    $this->email->to($this->input->post('email'));
    $this->email->subject($row_email->subject);
    $mail_message = $this->email_drafts_model->employer_signup($row_email->content, $employer_array);
    $this->email->message($mail_message);
    $this->email->send();

    redirect(base_url('employer/post_new_job'),'');
}

型号

public function add_company($data){

        $return = $this->db->insert('pp_companies', $data);
        if ((bool) $return === TRUE) {
            return $this->db->insert_id();
        } else {
            return $return;
        }       

}   

public function update_company($id, $data){
    $this->db->where('ID', $id);
    $return=$this->db->update('pp_companies', $data);
    return $return;
}

public function delete_company($id){
    $this->db->where('ID', $id);
    $this->db->delete('pp_companies');
    return true;
}



/*public function get_all_companies($per_page, $page) {
    $this->db->select('pp_companies.*');
    $this->db->from('pp_companies');
    $this->db->order_by("pp_companies.ID", "DESC"); 
    $this->db->limit($per_page, $page);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}*/

public function get_all_companies($per_page, $page) {
    $this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_phone, pp_companies.company_website, pp_companies.industry_ID, pp_companies.company_logo');
    $this->db->from('pp_companies');
    $this->db->join('pp_employers', 'pp_employers.company_ID = pp_companies.ID', 'inner');
    $this->db->order_by("pp_employers.ID", "DESC"); 
    $this->db->limit($per_page, $page);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function record_count($table_name) {
    return $this->db->count_all($table_name);
}

public function get_company_by_id($id) {
    $this->db->select('pp_companies.*');
    $this->db->from('pp_companies');
    $this->db->where('pp_companies.ID', $id);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function check_slug($slug) {

    $this->db->where('company_slug', $slug);
    $this->db->from('pp_companies');
    return $this->db->count_all_results();
}

public function check_slug_edit($CID, $slug) {

    $this->db->where('company_slug', $slug);
    $this->db->where('ID !=', $CID);
    $this->db->from('pp_companies');
    return $this->db->count_all_results();
}



public function get_company_by_old_id($id) {
    $this->db->select('pp_companies.*');
    $this->db->from('pp_companies');
    $this->db->where('pp_companies.old_company_id', $id);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

第二模型

public function add_employer($data){

        $return = $this->db->insert('pp_employers', $data);
        if ((bool) $return === TRUE) {
            return $this->db->insert_id();
        } else {
            return $return;
        }       

}   

public function update_employer($id, $data){
    $this->db->where('ID', $id);
    $return=$this->db->update('pp_employers', $data);
    return $return;
}

public function update($id, $data){
    $this->db->where('ID', $id);
    $return=$this->db->update('pp_employers', $data);
    return $return;
}

public function delete_employer($id){
    $this->db->where('ID', $id);
    $this->db->delete('pp_employers');
}

public function authenticate_employer($user_name, $password) {
    $this->db->select('pp_employers.*, pp_companies.company_slug');
    $this->db->from('pp_employers');
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
    $this->db->where('email', $user_name);
    $this->db->where('pass_code', $password);
    $this->db->limit(1);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function authenticate_employer_by_email($user_name) {
    $this->db->select('pp_employers.*');
    $this->db->from('pp_employers');
    $this->db->where('email', $user_name);
    $this->db->limit(1);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function authenticate_employer_by_password($ID, $password) {
    $this->db->select('*');
    $this->db->from('pp_employers');
    $this->db->where('ID', $ID);
    $this->db->where('pass_code', $password);
    $this->db->limit(1);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function is_email_already_exists($ID, $email) {
    $this->db->select('ID');
    $this->db->from('pp_employers');
    $this->db->where('ID !=', $ID);
    $this->db->where('email', $email);
    $this->db->limit(1);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row('ID');
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}


public function get_all_employers($per_page, $page) {
    $this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_employers.city, pp_employers.country, pp_employers.top_employer, pp_employers.ip_address, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_logo, pp_companies.company_phone, pp_companies.company_location, pp_companies.company_slug');
    $this->db->from('pp_employers');
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
    $this->db->order_by("pp_employers.ID", "DESC"); 
    $this->db->limit($per_page, $page);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function record_count($table_name) {
    return $this->db->count_all($table_name);
}

public function get_employer_by_id($id) {
    $this->db->select('pp_employers.*, pp_companies.ID AS CID,pp_companies.company_name,pp_companies.company_email,pp_companies.ownership_type,pp_companies.company_ceo,pp_companies.industry_ID,pp_companies.ownership_type,pp_companies.company_description,pp_companies.company_location,pp_companies.no_of_offices,pp_companies.company_website,pp_companies.no_of_employees, pp_companies.established_in, pp_companies.company_logo, pp_companies.company_folder, pp_companies.company_type, pp_companies.company_fax, pp_companies.company_slug, pp_companies.company_phone, pp_job_industries.industry_name');
    $this->db->from('pp_employers');
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
    $this->db->join('pp_job_industries', 'pp_companies.industry_ID = pp_job_industries.ID', 'left');
    $this->db->where('pp_employers.ID', $id);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function get_employer_by_id_simple($id) {
    $this->db->select('pp_employers.*');
    $this->db->from('pp_employers');
    $this->db->where('pp_employers.ID', $id);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function get_employer_by_company_id($cid) {
    $this->db->select('pp_employers.*, pp_companies.ID AS CID,pp_companies.company_name,pp_companies.company_email,pp_companies.company_ceo,pp_companies.industry_ID,pp_companies.ownership_type,pp_companies.company_description,pp_companies.company_location,pp_companies.no_of_offices,pp_companies.company_website,pp_companies.no_of_employees, pp_companies.established_in, pp_companies.company_logo, pp_companies.company_folder, pp_companies.company_type, pp_companies.company_fax, pp_companies.company_phone');
    $this->db->from('pp_employers');
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
    $this->db->where('pp_employers.company_ID', $cid);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->free_result();
    return $return;
}

public function search_all_employers($per_page, $page, $search_parameters, $wild_card='') {

    $where = ($wild_card=='yes')?'where':'like';
    $this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_employers.top_employer, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_logo');
    $this->db->from('pp_employers');
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
    $this->db->$where($search_parameters);
    $this->db->order_by("pp_employers.ID", "DESC"); 
    $this->db->limit($per_page, $page);
    $Q = $this->db->get();
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->free_result();
    //echo $this->db->last_query(); exit;
    return $return;
}

public function search_record_count($table_name, $search_parameters) {
    //return $this->db->count_all($table_name);
    $this->db->like($search_parameters);
    $this->db->from($table_name);
    $this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
    return $this->db->count_all_results();
    //exit;
}
public function get_all_active_employers($per_page, $page) {
    $Q = $this->db->query("CALL get_all_active_employers($page, $per_page)");
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->next_result();
    $Q->free_result();
    return $return;
}   

public function get_all_active_top_employers($per_page, $page) {
    $Q = $this->db->query("CALL get_all_active_top_employers($page, $per_page)");
    if ($Q->num_rows > 0) {
        $return = $Q->result();
    } else {
        $return = 0;
    }
    $Q->next_result();
    $Q->free_result();
    return $return;
}


public function get_company_details_by_slug($slug) {
    $Q = $this->db->query('CALL get_company_by_slug("'.$slug.'")');
    if ($Q->num_rows > 0) {
        $return = $Q->row();
    } else {
        $return = 0;
    }
    $Q->next_result();
    $Q->free_result();
    return $return;
}   

我知道,我必须在 Controller 中使用 implode 函数并将 name 属性放入数组中,即 = Category1[],但我不太熟悉如何在这种情况下正确应用它,因为多次尝试执行此操作都导致 Controller 在页面中不显示任何内容。

如果我能得到一些帮助来解决这个问题,这会对我有很大帮助。

基本上,一旦将多个值插入数据库,我就会运行搜索和过滤器。

编辑 1:添加 Controller 和模型

最佳答案

试试这个

<div class="input-group <?php echo (form_error('category1'))?'has-error':'';?>">
<label class="input-group-addon">Category </label>
<select name="category1[]" class="js-example-basic-multiple form-control" multiple="multiple">
  <option value="Engineering" <?php echo (set_value('category1')=='Engineering')?'selected':''; ?>>Engineering</option>
  <option value="Management" <?php echo (set_value('category1')=='Management')?'selected':''; ?>>Management</option>
  <option value="Law" <?php echo (set_value('category1')=='Law')?'selected':''; ?>>Law</option>
</select>

关于php - 在 CodeIgniter 中内爆 Select2 表单中的多个值并插入到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43557168/

相关文章:

php - 几个模拟连接对数据库的影响

php - 如何使用 php 创建嵌套 JSon

sql - 优化 SQL 距离查询

mysql使用交叉连接连接三个表错误

php - 在 Symfony 3.4 中覆盖默认的 FOSUserBundle Controller

php - 查找/检测/格式化周围文件中使用的 GetText 字符串

php - CodeIgniter POST 在本地主机上工作,但在实时服务器上不工作

Gas ORM 中的 Mysql 查询

php - 将数据从表单值传递到 ajax 调用 (Javascript)

php - 让 Ajax 提交表单数据,但不会为文本区域执行此操作