2017-07-19 99 views
0

我想問一下,如何從URL發佈ID到數據庫,我可以在最後一個URL中看到這個ID。 我的網址:Codeigniter:如何從URL獲得ID

http://localhost/rekammedis/index.php/pasien/tambah_rm/3 

我使用URI來獲得$ id_pasien價值,但是當我上傳到數據庫中該值爲空/空就不貼了。

我的模型:

function tambahrm($id) 
{ 
    $this->load->helper('url'); 
    $username = trim($this->session->userdata('id')); 
    $id_pasien = $this->uri->segment(3); 
    $data = array(
     'tgl_berobat' => $this->input->post('tgl_berobat'), 
     'anamnesa' => $this->input->post('anamnesa'), 
     'diagnosa' => $this->input->post('diagnosa'), 
     'therapi' => $this->input->post('therapi'), 
     'keterangan' => $this->input->post('keterangan'), 
     'id_user' => $username, 
     'id_pasien' => $id_pasien, 
    ); 

    if ($id == 0) { 
     return $this->db->insert('tbl_riwayat', $data); 
    } else { 
     $this->db->where('id', $id); 
     return $this->db->update('tbl_riwayat', $data); 
    } 
} 

這裏是結果: Id_pasien remains unfilled.

+0

嘗試把回聲確保ü從URL中所獲得的價值......通過參考笨URL結構,將分配給功能參數本身.. 。因此,檢查'$ id'是否得到值。 –

+0

另請參閱:https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string – Dalton

+0

你在這裏發送什麼函數tambahrm($ id)< - ?因爲你可以做 $ id_pasien = $ id;你應該得到3. –

回答

1

如果你使用URI路由,還可以檢索有關重新路由段的信息:

$this->uri->segment(n); 

在哪裏n是您希望檢索的段號。

你的情況,那就是:

$this->uri->segment(3); 
+0

still ** null **在數據庫中。 –

+0

您是否嘗試在插入前打印它以查看您獲得的值? –

+0

@Just_Do_It錯了。第一段是'index.php'後面的第一段。所以** 3 **是最終答案。 – Tpojka