0

I am using uri segment to delete info in my database:

anchor('site/delete_note/'.$row->id, 'Delete')

Model:

function delete_note()
{
    $this->db->where('id', $this->uri->segment(3));
    $this->db->delete('note');
}

It works fine, but I want to do the same for updating my info and can't get it work So this is link in view:

anchor('site/edit_note/'.$row->id, 'Edit')  

My controller:

function edit_note()
{
    $note_id = $this->uri->segment(3);
    $data['main_content'] = 'edit_note';
    $this->load->view('includes/template', $data);

    $this->load->library('form_validation');

    $this->form_validation->set_rules('content', 'Message', 'trim|required');


    if($this->form_validation->run() == TRUE)
    {
        $this->load->model('Note_model');
        $this->Note_model->edit_note($note_id);
        redirect('site/members_area'); 

    }

}

My model:

function edit_note($note_id)
{
    $content = $this->input->post('content');
    $data = array('content' => $content);
    $this->db->where('id', $note_id);
    $this->db->update('note', $data);
}

My view of edit_note:

<?php

    echo form_open('site/edit_note');
    echo form_textarea('content', set_value('content', 'Your message'));

    echo form_submit('submit', 'Change');
    echo anchor('site/members_area', 'Cancel');


    echo validation_errors('<p class="error">');        ?>

Edit doesn't work as delete, when i am trying to get segment directly in edit model, as I used in delete model.

If I set $note_id to a number in my controller, instead of this '$this->uri->segment(3)', it updates my database. But if I use getting segment it doesn't work. I thought uri segments are available in controller as in model, but there is something I don't know.

3
  • this should work fine i dont see any error in your code Commented Sep 20, 2012 at 7:53
  • sadly it doesn't :( as I said, updating works if I change this "$note_id = $this->uri->segment(3)" to "$note_id = 24" (I use id that exist in my database) in my controller. So something wrong with getting segment Commented Sep 20, 2012 at 8:00
  • see my answer and the edits in the answer Commented Sep 20, 2012 at 8:06

3 Answers 3

1

Better yet, instead of manually reading the IDs via the segments, you could change your functions to be:

function delete_note($note_id)

and

function edit_note($note_id)

And remove the $note_id = $this->uri->segment(3); lines.

And as silly as it'll sound, the generated URL is definitely correct, right?

And last question, have you done anything with routes?

Edit

I've also noticed that in edit, you use this in your form:

echo form_open('site/edit_note');

So when the form submits, the URL it submits to is site/edit_note instead of site/edit_note/{SOME_ID}. So once you make your changes, and the form submits, there won't be a 3rd URL segment!

Sign up to request clarification or add additional context in comments.

8 Comments

I use this echo in my edit_note view to check if I get that number I need: $note_id = $this->uri->segment(3); echo "$note_id"; It prints that I need
Is this when you open the edit form (after you click the edit link) or after the form is submitted and you are trying to save it?
Yeah, so stupid mistake, I changed it to echo form_open('site/edit_note/' .$this->uri->segment(3)); in my edit_note view. And it works!
The other solution would have been to just leave that blank (so echo form_open();). Then the browser will always submit the form to whatever the current URL is (so you don't have to manually reconstruct it every time) :)
So it is enough to do the same as I have done in my delete method now. There is no need to do all the segment passing from controller to model, I can access it directly from my model
|
1

Well there are some logical errors in your code.

function edit_note()
{
    $note_id = $this->uri->segment(3);
    $data['main_content'] = 'edit_note';
    $this->load->view('includes/template', $data);
//// What is the use of loadig a view when you are editing

    $this->load->library('form_validation');

    $this->form_validation->set_rules('content', 'Message', 'trim|required');


    if($this->form_validation->run() == TRUE)
    {
        $this->load->model('Note_model');
        $this->Note_model->edit_note($note_id);
        redirect('site/members_area'); 

    }

}

Instead do it like this

function edit_note()
{
    if($this->input->post()){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('content', 'Message', 'trim|required');


        if($this->form_validation->run() == TRUE)
        {
            $this->load->model('Note_model');
            $this->Note_model->edit_note($note_id);
            redirect('site/members_area'); 
        }
    }else{
        $note_id = $this->uri->segment(3);
        $data['main_content'] = 'edit_note';
        $this->load->view('includes/template', $data);
    }
}

MOST IMPORTANT
And the other thing you should note that you are using anchor to access edit note but not actually submitting a form so it is not getting any post data to update.

4 Comments

I load view, that user can have a form, there he edits message. I need to show old message in that text area too, but haven't done that yet
print_r($this->input->post()) to see if the post data is coming just for debugging purpose
I wrote before: "updating works if I change this "$note_id = $this->uri->segment(3)" to "$note_id = 24" (I use id that exist in my database) in my controller" so I guess it gets post data :)
then do it echo $this->uri->segment(3); because 3rd segment might not having any value also check your view file anchor for update it should have been 'site/members_area/some_id' instead of 'site/members_area/'
0

In my view it's a 'bad' approach to use uri segments in your models... you should pass an id as a parameter from your controller functions ..

function delete_note()
{
    $this->db->where('id', $this->uri->segment(3));
    $this->db->delete('note');
}

What if you want to re-use this delete method? e.g. deleting notes from an admin panel, via a cron job etc then the above relies upon the uri segment and you will need to create additional delete methods to do the job. Also, if you were to continue with the same you don't even need a model then .. just call these lines in your controllers if you know what I mean ...

$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');

so best is to change it to similar to your edit_note() model function.

3 Comments

But I thought model is for operations with database. It is not recommended to use controller for operations with data.
Yes, that's correct but you're binding your model with URI in your code above - and as per your current logic there isn't any control over who can delete this data etc .. I can just go to yourwebsite.com/delete_note/1 .. /delete_note/2, /3/, /4 and mess all your data .. but if you're passing these ID via controller you can sanitise this value, check if the user has permissions to delete the note and many more things as required. Hope it makes sense.
Yeah it makes sense now. I will surely use it in the future, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.