php - Updating with codeigniter -
i finished tutorial making news application on codeigniter user_guide. trying extend tutorial website loads text textarea can retyped , used update entry. want view article page same page updating. view loads correctly, when click submit, brings me localhost/ci/news/foo localhost/ci/news/view along 404. have tried imitate create, , altering model query, since i've been struggling while, have tried several ideas.
i suspect problem 1 of following.
something directing me ci/news/view misusing $slug variable misconnecting controller , model syntax in model query.
i'll post relevent code.
here's controller function view, updates happen.
public function view($slug) { $this->load->helper('form'); $this->load->library('form_validation'); $data['news_item'] = $this->news_model->get_news($slug); $slug = $slug; $this->form_validation->set_rules('text', 'text', 'required'); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; if ($this->form_validation->run() === false) { $this->load->view('templates/header', $data); $this->load->view('news/view', $data); $this->load->view('templates/footer'); } else { $this->news_model->update_news(); $this->load->view('news/success'); } }
here's update_news() in model
public function update_news($slug) { $data = array( 'slug' => $slug, 'text' => $this->input->post('text') ); $this->db->set('slug', $slug); $this->db->update('news', $data); }
and view.php
<?php echo validation_errors(); echo form_open('news/view'); echo '<h2>'.$news_item['title'].'</h2>'; ?> <label for="text">text</label> <textarea name="text"><?php echo $news_item['text']; ?></textarea><br /> <input type="submit" name="submit" value="update" /> </form>
update: requested, get_news model function.
public function get_news($slug = false) { if ($slug === false) { $query = $this->db->get('news'); return $query->result_array(); } $query = $this->db->get_where('news', array('slug' => $slug)); return $query->row_array(); }
in model put instead of set.
public function update_news($slug) { $data = array( 'slug' => $slug, 'text' => $this->input->post('text') ); $this->db->where('slug', $slug); $this->db->update('news', $data); }
Comments
Post a Comment