Codeigniter活动记录/更新表中的一行

时间:2013-02-03 13:13:05

标签: php codeigniter

大家好,我有以下型号,我需要更新字段:

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. 

1 个答案:

答案 0 :(得分:3)

如果要更新数据库中的现有记录,则需要将where子句传递给php函数,如:

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;
                }

    }

因此,$ value标准可能是现有评论的ID,例如review_id或其他任何内容。

事件更好:

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

因此,如果您的条件与表格中的1行匹配,则只会更新1行。