0

I want to ask, how to post ID to database from URL, I can see this ID in the last url. my Url :

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

I'm using uri to get $id_pasien value but when I post to database this value is null/empty not posted.

my model :

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

here is result : Id_pasien remains unfilled.

one_n00b
  • 71
  • 1
  • 2
  • 7
  • Try to put echo to make sure u getting the value from the url... By referring codeigniter url structure, it will assign to the function parameter itself... So check `$id` whether it getting the value or not. –  Jul 19 '17 at 17:10
  • See also: https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string – Dalton Cézane Jul 19 '17 at 17:10
  • what do you send here function tambahrm($id) <-- ? because you could just do $id_pasien = $id; you should be getting 3. – Rildo Gomez Jul 19 '17 at 18:03
  • Half of that code should be in controller. Model shouldn't cooperate with input class but receive variable from controller instead. What are you getting when you put this in model: `exit($this->uri->segment(3));`? – Tpojka Jul 19 '17 at 18:42

1 Answers1

1

If you use URI routing, you can also retrieve information about the re-routed segments:

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

Where n is the segment number you wish to retrieve.

In your case it would be:

$this->uri->segment(3);
Just_Do_It
  • 821
  • 7
  • 20