php - URI segment can't be saved in database -


i trying insert new record in database table. function insert this:

public function comment($id_b,$comment){ $id_u = $this->session->userdata('userid'); $result=$this->db->insert('comments',['id_book'=>$id_b, 'id_user'=>$id_u, 'comment'=>$comment] ); return $result; } 

the controller function comment called:

public function comment() { $this->load->model('model'); $id_b= $this->uri->segment('3'); $comment=$this->input->post('comment'); $this->model->comment($id_b,$comment); } 

the uri segment i'm trying id. problem can't saved in database, has value null.

i tried echo $this->uri->segment('3'); , returns id, don't understand why saved null value in database.

when tried this: (int)$this->uri->segment('3'); , got error:

error number: 1452

cannot add or update child row: foreign key constraint fails (db.comments, constraint comments_ibfk_1 foreign key (id_book) references book (id_book))

insert comments (id_book, id_user, comments) values (0, '5', 'test')

so, tries insert book id_book=0 when in fact there no such record in book table , have no idea gets id_book=0 because $this->uri->segment(3) returns 1.

there solution, , that's $id_b value parameter of controller method routed to. based on comments above, appears uri string search_results/result/1. means routing result method, in search_results controller. $id_b 1.

in search_results.php controller:

/**  * result  * @param  int $id_b  */ public function result( $id_b ) {     // pass $id_b comment method     // or whatever method calls comment method     $this->comment( $id_b ); }  /**  * comment  * @param  int $id_b  */ public function comment( $id_b ) {     $comment = $this->input->post('comment');     $this->load->model('model');      // continue passing $id_b model     $this->model->comment( $id_b, $comment ); } 

the code in model can remain same.

/**  * comment  * @param  int $id_b  * @param  string $comment  */ public function comment( $id_b, $comment ) {     $id_u = $this->session->userdata('userid');      return $this->db->insert('comments',[         'id_book' => $id_b,          'id_user' => $id_u,          'comment' => $comment     ]); } 

i can't tell why having problems getting 3rd segment of uri $this->uri->segment(3), work-around allows continue.

if me, in order fix uri segment issue, expirament changing config value uri_protocol in /application/config/config.php. while you're there, make sure setting valid base_url. in past versions, did not have set base_url, codeigniter says mandatory. i've been working codeigniter since v1.7.2 (2009), , never seen problem uri segments. if advice doesn't point in right direction, suggest sharing information codeigniter config.php file, , server.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -