Welcome Guest, Not a member yet? Register   Sign In
An Error Was Encountered
#1

[eluser]Matt Crowson[/eluser]
In the process of building a simple blog, I was validating a comment post and received this error:

You must use the "set" method to update an entry.

When I remove the validation and just have the comments post straight into the database I have no problem. Anyone have any ideas?


Controller
Code:
class Blog extends Controller {
    
    function Blog()
    {
        parent::Controller();
        
        $this->load->model('blog_model');
        
        $this->load->library('validation');
        
        $this->load->scaffolding('entries');    
        
        $this->load->helper('url');
        $this->load->helper('form');
        

    }
    
    function index()
    {
        $data['heading'] = "Blog";
        $data['title'] = "Matthew Crowson";
        $data['query'] = $this->db->get('entries');
        
        $this->load->view('blog/blog', $data);
    }
    
    function comments()
    {
        $this->load->library('validation');
            
        $data['heading'] = "Comments";
        $data['title'] = "Matthew Crowson";
        
        $this->db->where('entry_id', $this->uri->segment(3));
        $data['query'] = $this->db->get('comments');
        $this->db->where('id', $this->uri->segment(3));
        $data['entry'] = $this->db->get('entries');
        
        $rules['author']    = "required";
        $rules['body']    = "required";
        $rules['email']    = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['author']    = 'Author';
        $fields['body']    = 'Comment';
        $fields['email']    = 'Email Address';
    
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('blog/comments', $data);
        }
        else
        {
            redirect('blog/comment_insert');
        }
    
    }
    
    function comment_insert()
    {
        $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
    }
}

form from the View file
Code:
<?=$this->validation->error_string; ?>

<?=form_open($this->uri->uri_string()); ?>
<?=form_hidden('entry_id', $this->uri->segment(3));?>

<h5>Author</h5>
&lt;input type="text" value="&lt;?=$this-&gt;validation->author;?&gt;" name="author"  size="50" />

<h5>E-mail</h5>
&lt;input type="text" value="&lt;?=$this-&gt;validation->email;?&gt;" name="email" size="50" />

<h5>Comment</h5>
&lt;textarea name="body" value="&lt;?=$this-&gt;validation->body;?&gt;" rows="10">&lt;/textarea&gt;
<br>
<div>&lt;input type="submit" value="Submit" /&gt;</div>

&lt;/form&gt;
#2

[eluser]nirbhab[/eluser]
Code:
else
        {
            redirect('blog/comment_insert');
        }
    
    }
    
    function comment_insert()
    {
        $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
    }

when you are redirecting in your comment method to comment_insert method, you will be losing your POSTe d data. than how can you insert any data.
check this
Code:
function comment_insert()
    {
print_r($_POST);//see what you get here.
        $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
    }
#3

[eluser]nirbhab[/eluser]
You can also do this:
Code:
if ($this->validation->run() == FALSE)
        {
            $this->load->view('blog/comments', $data);
        }
        else
        {
                $this->db->insert('comments', $_POST);
                redirect('blog/comments/'.$_POST['entry_id']);
                //REDIRECT TO SOME OTHER METHOD, AS IT IS COMING TO SELF METHOD.redirect('blog/comments/'.$_POST['entry_id']);
            //redirect('blog/comment_insert');
        }
#4

[eluser]Matt Crowson[/eluser]
thanks a lot, the second option worked well




Theme © iAndrew 2016 - Forum software by © MyBB