【问题标题】:my values on one column wont update immediately after inserting data, but after refresh the values shows up插入数据后,我在一列上的值不会立即更新,但刷新后会显示值
【发布时间】:2021-07-03 00:51:20
【问题描述】:

在我插入数据后,列表“Jumlah Hasil Perah”上显示的内容显示为“0”。但刷新浏览器后,值显示结果。

这是代码

型号(m_hasilperah):

public function jumlahPerahSapi($id)
{   
    $this->db->select('hasilPerahPagi, hasilPerahSore');
    $this->db->where('idSapi', $id);
    $cek = $this->db->get('tb_hasilperah');
    if ($cek) {
        $this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
        $this->db->where('idSapi', $id);
        $this->db->update('tb_hasilperah');
    }
    return false;
}

控制器

public function tambahHasilPerah(){
    $idSapi = $this->input->post('idSapi');
    $tglPerah = $this->input->post('tglPerah');
    $hasilPerahPagi = $this->input->post('hasilPerahPagi');
    $hasilPerahSore = $this->input->post('hasilPerahSore');
    $jumlahPerah = $this->m_hasilperah->jumlahPerahSapi($idSapi);;

    $data =
        [
            'idSapi' => $idSapi,
            'tglPerah' => $tglPerah,
            'hasilPerahPagi' => $hasilPerahPagi,
            'hasilPerahSore' => $hasilPerahSore,
            'jumlahPerah' => $jumlahPerah
        ];
    $insert = $this->m_hasilperah->tambahHasilPerahModel($data, $idSapi);
    if ($insert) {
        redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh');
    } else {
        echo 'gagal';
    }
}

截图:

View after insert(before manual refresh)

View after manual refresh

【问题讨论】:

  • 如果您编写在页面加载后执行的正常代码,则首先通过基本操作,然后您的数据将在页面加载后加载。如果你想要没有页面加载的数据,你必须使用 ajax。
  • 我认为我对“redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh')”部分非常有信心......我不打算使用ajax .. . 只想知道为什么需要刷新这些值才能显示结果。 bcs 在其他功能上我不需要这样做。所以我认为问题出在模型上。

标签: php database codeigniter model-view-controller


【解决方案1】:

您的模型方法 jumlahPerahSapi() 每次都返回 false。即使在数据更新之后。请调查一下。

【讨论】:

    【解决方案2】:

    您的模型函数jumlahPerahSapi($id) 始终返回false,即使已成功插入数据库。 所以这不是运行redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh'); 每次运行echo 'gagal'; 修改模型函数如下。

    public function jumlahPerahSapi($id)
    {   
        $this->db->select('hasilPerahPagi, hasilPerahSore');
        $this->db->where('idSapi', $id);
        $cek = $this->db->get('tb_hasilperah');
        if ($cek) {
            $this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
            $this->db->where('idSapi', $id);
            $this->db->update('tb_hasilperah');
            
            return true;
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2016-06-15
      • 2022-01-19
      • 2014-02-13
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      相关资源
      最近更新 更多