如何在同一视图页面上插入和更新?

时间:2018-12-10 05:29:06

标签: php codeigniter insert

我的问题是当我单独执行插入和更新工作正常..但是当我在存在更新按钮的情况下执行插入操作时,我得到的是空值。如何在同一视图页面上执行插入和更新。 。 插入的控制器代码:

 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&hellip;" 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']; } ?>">

2 个答案:

答案 0 :(得分:0)

您可以通过('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');

在Daybook函数中获取ID,并使用select查询获取数据以传递视图文件。 示例:

 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']);
    }

这可能会对您有所帮助。

答案 1 :(得分:0)

您的数据将进入哪个数组。像这样使用该数组:尝试此操作,希望对您有所帮助。

<?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">