0

I'm new to Codeigniter and am having a problem understanding what I did wrong in my code for updating data in my database. I would really appreciate your input, since I have been wrecking my brains trying to figure out the problem. Thank you!

This is my controller:

public function edit(){

            $slug=basename($_SERVER['REQUEST_URI']);

            $data['news_item']=$this->news_model->get_news($slug);

            $id=$data['news_item']['id'];


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

            $data['title'] = 'Edit a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
                {
                    $this->load->view('templates/header', $data);
                    $this->load->view('news/edit');
                    $this->load->view('templates/footer');

                }
            else{

                $data = array(
                                'title' => $this->input->post('title'),
                                'text' => $this->input->post('text')
                            );

                $this->news_model->edit_news($id,$data); 
                $this->load->view('templates/header', $data);
                $this->load->view('news/success');
                $this->load->view('templates/footer');
                }   

    }

These are my models, one for getting the table row, and one for updating(apparently xD):

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();

    }

public function edit_news($id,$data){

        $this->db->where('id',$id);
        $this->db->update('news',$data);

        return;
    }

I have debugged (if you could call it that) the controller and the models, and checked all the important values by printing them or echoing them out, and those are all in order.

Nothing wrong in my opinion with my forms or anything. It all seems to be working fine until the actuall update is called (

$this->db->where('id',$id); 
$this->db->update('news',$data);

).

I can't even remember what I tried on this xD Thanks in advance for any input!

1 Answer 1

1

Try returning the update

return $this->db->update('news',$data);

Also where is your input for the id variable?

maybe do

$this->db->where('id', $this->input->post('id'))

So it looks like

$this->db->where('id', $this->input->post('id'));
return $this->db->update('news',$data);

But just try returning first.

PART 2 from comments on how to create a update function

public function edit_news($id,$data){

 $slug = url_title($this->input->post('title'));


  $data = array('title' => $this->input->post('title'),
              'slug' => $slug,
              'body' => $this->input->post('body')

                );

              $this->db->where('id', $this->input->post('id'));

              return $this->db->update('posts',$data);

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

9 Comments

Thanks for the reply but I tried that before, no result. My id is set as auto_increment so anytime I create a something new it sets itself automatically. I have echoed it out, it's definitely there before being used by the update() function.
Thank you very much!!!! It worked when I added the 'id' input!! So, it does work, but since the client wouldnt' be familiar with the id, how would I pass it into the function not using the form?
I am not really sure I understand what you are asking, normally if you are on the (post) the id function should automatically be predefined and should need no action from the user, Only just clicking the edit button to call the edit function.Please give me further clarification if you need a example of this. Also if my answer worked please mark it correct so I can get credit.
I gave a example above on how I normally pass data for a update function
That's exactly what I think as well. I don't want the user to have to enter the id of the data that is being edited. I want it to be "known" or "saved" just by entering the news/edit page. Let me explain...
|

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.