我的问题是,当我单独执行时,插入和更新工作正常。。但当我在出现更新按钮时插入时,我得到的是null值。。我如何在同一个视图页面上插入和更新。。插入控制器代码:
public function insert($id='')
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->insert('daybook',$data);
$this->session->set_flashdata('Add', 'Inserted Successfully');
redirect('BookKeeping/daybook','refresh');
}
更新的控制器代码:
public function update($id='')
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$id=$this->input->post('id');
echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->where('recno', $_POST['id']);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook','refresh');
}
查看插入和更新页面:
<input type="text" class="form-control input-xs datepicker-dates" placeholder="Pick a date…" id="TDate" name="TDate"value="<?php if(!empty($result) && !empty($result['date'])){ echo $result['date']; } ?>" required="">
<input type="text" id="TAmt" class="form-control input-xs qty" name="part1" style="width: 370px;" placeholder="Particulars" required value="<?php if(!empty($result) && !empty($result['part'])){ echo $result['part']; } ?>">
<input type="text" id="TAmt" class="form-control input-xs qty" name="part2" style="width: 370px;" placeholder="Particulars" required value="<?php if(!empty($result) && !empty($result['part'])){ echo $result['part1']; } ?>">
您可以通过('BookKeeping/daybook'.$last_insert_id'(传递上次插入id重定向
插入查询:
$this->db->insert('daybook',$data);
$this->session->set_flashdata('Add', 'Inserted Successfully');
redirect('BookKeeping/daybook/'.$last_insert_id,'refresh');
更新查询:更新id
$this->db->where('recno', $_POST['id']);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook/'.$_POST['id'],'refresh');
在日记本函数中获取id,并使用选择查询获取数据来传递视图文件。示例:
function daybook($id= "")
{
$this->db->select('*');
$this->db->from('daybook');
$this->db->where('recno', $_POST['id']);
$query = $this->db->get();
$result = $query->result_array();
$data['result'] = $result;
$this->load->view('daybook', $data['result']);
}
它可能会对你有所帮助。
数据来自哪个数组。像这样使用那个数组:试试这个,我希望它能帮助你。
<?php echo isset($arrayName) ? "Update" : "Add"; ?>
<input name="part" type="text" class="form-control" value="<?php echo isset($arrayName) && isset($arrayName['part']) ? $arrayName['part'] : '' ?>" id="part" placeholder="part">