【发布时间】: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