【问题标题】:codeigniter Can't insert field in databasecodeigniter无法在数据库中插入字段
【发布时间】:2016-05-22 21:19:35
【问题描述】:

我建立了我的网站,但有一些问题,首先,我无法插入数据库。

我试过了,但是没用。

请帮忙

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.'>'.$new->name.''; }

                                    ?>
                                </select>

【问题讨论】:

  • 做codeigniter手册中的教程,它将回答你的大部分问题,包括所有关于插入数据库的问题。下一步 - 当你正在构建新的东西时 - 从简单开始,然后再构建。就像在这种情况下,从几个表单字段开始。让它工作。然后添加其余字段。以这种方式发现错误要快得多。

标签: codeigniter insert userid


【解决方案1】:

要使用 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.将数据传递给模型,但要确保数据库表的字段名称与您发送的$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();
    }

希望对你有所帮助。

谢谢!

【讨论】:

  • 谢谢Abu Muaz,但是所有数据已经​​插入数据库,但是city_case字段没有插入,如果我用act_id等另一个字段替换这个字段,它插入正确,我确定问题在这个领域,你能帮帮我吗?
  • 当我改变类型 id)) $sel = "selected='selected'"; echo ''; }?>
  • @ahmedgamal 请尝试下面的代码 snap
猜你喜欢
  • 2017-10-23
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多