codeigniter无法在数据库中插入字段

时间:2016-05-22 21:19:36

标签: codeigniter insert userid

我构建了我的网站,但是存在一些问题,首先,我无法在数据库中插入。

我试图这样做,但它不起作用。

任何帮助,请

in controllers / cases.php


function add(){
		$data['fields_clients'] = $this->custom_field_model->get_custom_fields(1);
		$data['fields']			 = $this->custom_field_model->get_custom_fields(2);
		$data['clients']		 = $this->cases_model->get_all_clients();
		$data['stages'] 		 = $this->case_stage_model->get_all();
		$data['acts'] 			 = $this->cases_model->get_all_acts();
		$data['courts']			 = $this->cases_model->get_all_courts();
		$data['locations'] 		 = $this->cases_model->get_all_locations();
		$data['case_categories'] = $this->cases_model->get_all_case_categories();
		$data['court_categories']= $this->cases_model->get_all_court_categories();
                $data['city'] = $this->cases_model->get_name_city();
                //$data['employee'] = $this->cases_model->get_name_employee();
		if ($this->input->server('REQUEST_METHOD') === 'POST')
        {	
			$this->load->library('form_validation');
			$this->form_validation->set_message('required', lang('custom_required'));
			$this->form_validation->set_rules('title', 'lang:title', 'required');
			$this->form_validation->set_rules('client_id', 'Client', '');
			$this->form_validation->set_rules('case_no', 'Case No', '');        //trim|required|is_unique[cases.case_no]
			$this->form_validation->set_rules('location_id', 'Location', '');
			$this->form_validation->set_rules('case_stage_id', 'Case Stage', '');
			$this->form_validation->set_rules('court_id', 'Court', '');
			$this->form_validation->set_rules('court_category_id', 'Court Category', '');
			$this->form_validation->set_rules('case_category_id', 'Case Category', '');
			$this->form_validation->set_rules('act_id', 'Act', '');
			$this->form_validation->set_rules('start_date', 'Filing Date', '');
			$this->form_validation->set_rules('description', 'Description', '');
			$this->form_validation->set_rules('city_case', 'city case', '');
			$this->form_validation->set_rules('fees', 'Fees', '');
			$this->form_validation->set_rules('o_lawyer', 'Opposite Lawyer', '');
			$this->form_validation->set_rules('hearing_date', 'Description', '');
			 
			if ($this->form_validation->run()==true)
            {
				$save['title'] = $this->input->post('title');
				$save['case_no'] = $this->input->post('case_no');
				$save['client_id'] = $this->input->post('client_id');
				$save['location_id'] = $this->input->post('location_id');
				$save['court_id'] = $this->input->post('court_id');
				$save['court_category_id'] = $this->input->post('court_category_id');
				$save['case_stage_id'] = $this->input->post('case_stage_id');
				$save['case_category_id'] = json_encode($this->input->post('case_category_id'));
				$save['act_id'] = json_encode($this->input->post('act_id'));
				$save['description'] = $this->input->post('description');
				$save['start_date'] = $this->input->post('start_date');
				$save['hearing_date'] = $this->input->post('hearing_date');
				$save['o_lawyer'] = $this->input->post('o_lawyer');
				$save['fees'] = $this->input->post('fees');
				$save['city_case'] = json_encode($this->input->post('city_case'));
             
			 	$p_key = $this->cases_model->save($save);
				$reply = $this->input->post('reply');
					if(!empty($reply)){
					foreach($this->input->post('reply') as $key => $val) {
						$save_fields[] = array(
							'custom_field_id'=> $key,
							'reply'=> $val,
							'table_id'=> $p_key,
							'form'=> 2,
						);	
					
					}	
					$this->custom_field_model->save_answer($save_fields);
				}
				
                $this->session->set_flashdata('message', lang('case_created'));
				redirect('admin/cases');
			
				
			}
		}		
		
		
		$data['page_title'] = lang('add') . lang('case');
		$data['body'] = 'case/add';
		
		
		$this->load->view('template/main', $data);	

	}	
in view / add.php


<select name="city_case[]" class="chzn col-md-12" multiple="multiple" >
									<option value="">--<?php echo lang('select_city')?>--</option>
									<?php foreach($city as $new) {
											$sel = "";
											if(set_select('city_case', $new->id)) $sel = "selected='selected'";
											echo '<option value="'.$new->id.'" '.$sel.'>'.$new->name.'</option>';
										}
										
										?>
									</select>
models / cases_model.php


function get_name_city ()
	{
	 $query = $this->db->get ('user_role');
	 return $query->result();
	}

我用另一个代码替换add.php的代码,他工作得很好

- -                                     id))$ sel =“selected ='selected'”;                                             echo'id。''''。$ sel。'&gt;'。$ new-&gt; name。'';                                         }

                                    ?>
                                </select>

1 个答案:

答案 0 :(得分:0)

要使用codeigniter将数据插入数据库表,您必须将序列维持为 1.从视图到控制器获取值,您已正确完成

            $save['title'] = $this->input->post('title');
            $save['case_no'] = $this->input->post('case_no');
            $save['client_id'] = $this->input->post('client_id');
            $save['location_id'] = $this->input->post('location_id');
            $save['court_id'] = $this->input->post('court_id');
            $save['court_category_id'] = $this->input->post('court_category_id');
            $save['case_stage_id'] = $this->input->post('case_stage_id');
            $save['case_category_id'] = json_encode($this->input->post('case_category_id'));
            $save['act_id'] = json_encode($this->input->post('act_id'));
            $save['description'] = $this->input->post('description');
            $save['start_date'] = $this->input->post('start_date');
            $save['hearing_date'] = $this->input->post('hearing_date');
            $save['o_lawyer'] = $this->input->post('o_lawyer');
            $save['fees'] = $this->input->post('fees');
            $save['city_case'] = json_encode($this->input->post('city_case'));

2.将数据传递给模型,但要确保Database表的字段名称与发送 $ save [] 数组索引相同。

$p_key = $this->cases_model->save($save);

3.写入您的数据插入适当的活动记录或RAW sql代码,这在您给定的样本中是不正确的。请按照以下代码激活记录代码......

  public function save($save){
        $this->db->insert('YOUR_TARGETED_TABLE_NAME',$save);
        return $this->db->insert_id();
    }

希望它对你有所帮助。

谢谢!