Welcome Guest, Not a member yet? Register   Sign In
crud - help with updating [Solved!]
#1

[eluser]Mantra of Doom[/eluser]
Hi everyone,
I feel like such a noob being stuck like this, but I'm working on a blog application and having trouble updating posts. I can view the post, click the edit link and my editing form comes up populated with data. Great! But, as soon as I hit the submit button, things seem to stop working.

I'm sure it's because my code is a little convoluted or I'm doing things the long way, but I thought I'd ask now before I tear all my hair out.

The Controller
Code:
// Update Post
    function edit(){
        $this->load->helper('url');
        $this->load->model('news_model');
        $data = $this->news_model->general();
        $data['url_title'] = $this->uri->segment(3); //segment 3 is the clean url slug
        $data['post'] = $this->news_model->get_entry($data['url_title']); //fetch data from this entry to fill form

            if($this->input->post('submit')){
                $this->news_model->update(); //calls input function to post
                redirect('news/page', 'refresh'); //after hitting submit, load the news page and refresh.
            }
        
        $data['websubtitle'] = 'Edit News Post';    //change page header
        $data['content'] = 'news/news_edit'; //content
        $this->load->view('template', $data);    //load template
    }

The Model
Code:
// Gets a single entry based on its url title
    public function get_entry($url_title) {
        $this->db->select('entry_id, title, entry, author, creation_date, url_title')->where('url_title', $url_title);
        $query = $this->db->get('news', 1);

        if ($query->num_rows() == 1) {
            return $query->row();
        } else {
            return false;
        }
    }
    
    // Function to update the news post
    function update(){
        $this->load->database();
        $data = array(
                'summary'=>$this->input->post('entry'), //post first 250 characters from entry to summary
                'entry'=>$this->input->post('entry'), //posts the entry
                'title'=>$this->input->post('title'), //posts the title
                'url_title'=>$this->input->post('url_title'), //non-editable, but from hidden form
                'author'=>$this->input->post('author'), //calls author name
                );
        $this->db->set('creation_date', 'NOW()', FALSE);  //changes post date
        $this->db->where('url_title', $url_title);
        $this->db->update('news',$data);
    }
    function general(){
        $user = $this->ion_auth->get_user(); //pulls username from ion auth
        $data['tab']            = 'section-1'; //This which submenu is shown
        $data['summary']        = 'Summary';
        $data['entry']            = 'Entry';
        $data['author']            = 'Author';
        $data['title']            = 'Title';
        $data['url_title']        = 'URL Title';
        $data['creation_date']    = 'Creation Date';
        
        $data['fid']            = 0;
        $data['fsummary']        = array('name'=>'summary',
                                    'rows'=>5,
                                    'cols'=>80
                              );
        $data['fentry']            = array('name'=>'entry',
                                    'rows'=>10,
                                    'cols'=>80
                              );    
        $data['ftitle']         = array('name'=>'title',
                                    'size'=>30
                              );
        $data['furl_title']         = array('name'=>'url_title',
                                    'size'=>30
                              );
        // This will pull the author's name into the database when posting if the user is logged in.
        if ($this->ion_auth->logged_in())
        {
             $data['fauthor']['value'] = $user->username;
        }
        else
        {
            $data['fauthor']['value'] = 0;
        
        }        
        return $data;
    }

The View
Code:
<h3 class="blog">Edit Post </h3>
&lt;?php echo form_open('news/edit/'); ?&gt;
<p>Title: &lt;?php echo form_input('ftitle', $post->title);?&gt;</p>
<p>Entry:</p>
&lt;?php echo form_hidden('author',$fauthor['value']); ?&gt;
&lt;?php echo form_hidden('url_title',$url_title);?&gt;
<p>&lt;?php echo form_textarea('fentry', $post->entry); ?&gt;</p>
<p>&lt;?php echo form_submit('submit', 'Submit'); ?&gt;</p>
&lt;?php echo form_close(); ?&gt;

Thanks in advance for any and all help/advice!
#2

[eluser]xatrix[/eluser]
What error messages are you getting?

Also, enable profiling in your controller to better see what's going on.
#3

[eluser]Mantra of Doom[/eluser]
This is the error that I've been getting
Code:
Severity: Notice

Message: Undefined variable: url_title

Filename: models/news_model.php

Line Number: 73
Line 73:
Code:
$this->db->where('url_title', $url_title);

So it seems that when I click submit, I'm losing my $url_title variable

$url_title is defined in my controller right above my submit button like this:
Code:
$data['url_title'] = $this->uri->segment(3); //segment 3 is the clean url slug

By using the profiler, when I click the submit button, my uri segment looks like this: news/edit instead of :news/edit/post
I guess I need to find a way to pass that variable somehow, but I'm not quite sure how.
#4

[eluser]xatrix[/eluser]
Well, you're using $url_title, but you haven't defined it in the scope of your update() function. Since it's post data, it should be

Code:
$this->db->where('url_title', $this->input->post('url_title'));
#5

[eluser]Mantra of Doom[/eluser]
It works now! I think I get to a point where I look at the code too long and just miss some things. I love the profiler class now. I'm sure it will be my best friend with all these forms.




Theme © iAndrew 2016 - Forum software by © MyBB