0

大家好,我有以下模型,其中包含我需要更新的字段:

public function admin_add_review( $review = array()){

            $data = array(

                'title' => $review['title'],
                'description' => $review['description'],
                'category' => $review['category']
            );


            $query = $this->db->update('reviews', $data);

                if( $query){
                    return true;
                }else{
                    return false;
                }

    }

和下面的控制器,这里我只是将数据发送到视图。

public function admin_validation()
    {
        $this->load->library('form_validation');
        $this->load->model('model_users');

        $review['title'] = $this->input->post('title');
        $review['description'] = $this->input->post('description');
        $review['category'] = $this->input->post('category');
        $this->model_users->admin_add_review( $review );



        $this->load->view('admin_page', $review);

    }

和以下观点。这是一个表格,其中包含从控制器获取的数据,当我更新此数据时,它会使用内部相同的描述更新孔表。

<?php   
                            $review = $review[0];

                            echo form_open_multipart('main/admin_validation');

                            echo validation_errors();


                            /*echo "<p> Id review</p>";
                            $data = array(
                                "name" => "id",
                                 "value" => $review['id']
                            );
                            echo form_input($data);*/ 

                            echo "<h3>Title: ";                            
                            echo "</h3>";

                            $data = array(
                                "name" => "title",
                                 "id" => "title-input",
                                 "value" => $review['title']
                            );
                            echo form_input($data);


                            echo "<h3>Category: ";                            
                            echo "</h3>";
                            $data = array(
                                "name" => "category",
                                 "id" => "category-input",
                                 "value" => $review['category']

                            );  

                            echo form_input($data);


                            echo "<h3>Description: ";
                            echo "</h3>";
                            $data = array(
                              "name" => "description",
                              "id" => "description",
                              "value" => $review['description']

                            );

                            echo form_textarea($data);

                            echo "<br/>";
                            echo "<br/>";

                            echo form_submit('login_submit', 'Update review.');
                            echo form_close();              
            ?> 

     When i press the update button it updates all my database table rows! and i just want only the specified one to be updated. Help please. 
4

1 回答 1

3

If you want to update existing records from the database, you need to pass a where clause to your php function like:

public function admin_add_review($value, $review = array()){

            $data = array(

                'title' => $review['title'],
                'description' => $review['description'],
                'category' => $review['category']
            );

            $this->db->where('key', $value);
            $query = $this->db->update('reviews', $data);

                if( $query){
                    return true;
                }else{
                    return false;
                }

    }

So the $value criteria could be an id of existing review like review_id or anything else.

Event better:

$query = $this->db->update('reviews', $data, array('review_id' => $value));
//$query = $this->db->update(TABLE, NEW_DATA, CRITERIA);  

So if your criteria matches 1 row in table, then only 1 row will be updated.

于 2013-02-03T13:54:27.177 回答