0

因为这个,我把头撞到墙上

我知道这是一个很长的问题,但这就是我所拥有的:(

我与 CI 合作一年多,但这个错误让我很生气问题是我从模型中获取数据,所以它返回

Unknown column 'albums' in 'where clause'</p><p>SELECT *
FROM (`albums`)
WHERE `id` =  '1'
AND `albums` IS NULL
AND `id` =  '1'

模型函数为

function get($id = 0 )
   {
        if($id == 0 )
        {
            $q =  $this->db->get('albums')->result(); 
            return $q; 
        }
        else
        {
            $this->db->where("id",$id); 
            $q = $this->db->get('albums'); 
            return $q->row(); 
        }
   }

如果我删除了对函数的调用,当我调试 $data 时,我会从 json_encode 收到另一个错误,我会得到以下信息

Array
(
    [response] => CI_DB_mysql_driver Object
        (
            [dbdriver] => mysql
            [_escape_char] => `
            [_like_escape_str] => 
            [_like_escape_chr] => 
            [delete_hack] => 1
            [_count_string] => SELECT COUNT(*) AS 
            [_random_keyword] =>  RAND()
            [use_set_names] => 
            [ar_select] => Array
                (
                )

            [ar_distinct] => 
            [ar_from] => Array
                (
                )

            [ar_join] => Array
                (
                )

            [ar_where] => Array
                (
                    [0] => `id` =  '1'
                    [1] => AND `albums` IS NULL
                )

            [ar_like] => Array
                (
                )

            [ar_groupby] => Array
                (
                )

            [ar_having] => Array
                (
                )

            [ar_keys] => Array
                (
                )

            [ar_limit] => 
            [ar_offset] => 
            [ar_order] => 
            [ar_orderby] => Array
                (
                )

            [ar_set] => Array
                (
                    [`title`] => ' asdasd'
                    [`description`] => 'Hello '
                )

            [ar_wherein] => Array
                (
                )

            [ar_aliased_tables] => Array
                (
                )

            [ar_store_array] => Array
                (
                )

            [ar_caching] => 
            [ar_cache_exists] => Array
                (
                )

            [ar_cache_select] => Array
                (
                )

            [ar_cache_from] => Array
                (
                )

            [ar_cache_join] => Array
                (
                )

            [ar_cache_where] => Array
                (
                )

            [ar_cache_like] => Array
                (
                )

            [ar_cache_groupby] => Array
                (
                )

            [ar_cache_having] => Array
                (
                )

            [ar_cache_orderby] => Array
                (
                )

            [ar_cache_set] => Array
                (
                )

            [ar_no_escape] => Array
                (
                )

            [ar_cache_no_escape] => Array
                (
                )

            [username] => root
            [password] => 123456
            [hostname] => localhost
            [database] => sawt
            [dbprefix] => 
            [char_set] => utf8
            [dbcollat] => utf8_general_ci
            [autoinit] => 1
            [swap_pre] => 
            [port] => 
            [pconnect] => 1
            [conn_id] => Resource id #30
            [result_id] => 1
            [db_debug] => 1
            [benchmark] => 0.00059294700622559
            [query_count] => 1
            [bind_marker] => ?
            [save_queries] => 1
            [queries] => Array
                (
                    [0] => UPDATE `pulling_questions` SET `status` = 0 WHERE `status` =  1 AND `end_date` <=  1359556250
                )

            [query_times] => Array
                (
                    [0] => 0.00059294700622559
                )

            [data_cache] => Array
                (
                )

            [trans_enabled] => 1
            [trans_strict] => 1
            [_trans_depth] => 0
            [_trans_status] => 1
            [cache_on] => 
            [cachedir] => 
            [cache_autodel] => 
            [CACHE] => 
            [_protect_identifiers] => 1
            [_reserved_identifiers] => Array
                (
                    [0] => *
                )

            [stmt_id] => 
            [curs_id] => 
            [limit_used] => 
            [stricton] => 
        )

)

get上面的代码在另一个控制器中运行良好,如果我从视图中调用,模型的调用不会出现任何错误

我在此之前调用更新,它也不会更新

function update($id,$title,$description,$path)
    {
        $this->db->where('id',$id); 

        $this->db->set('title',$title);
        $this->db->set('description',$description);
        if($path != 'none')
            $this->db->set('path',$path);
        $q1 = $this->db->where('albums');


        return $q1;   
    }

这是控制器方法

public function update2($action = "show",$id = 0 ,$row = 0 )
    {
        $data = array();
        if($action == "show"):
            $data['id'] = $id; 
            $data['row_number'] = $row; 
            $this->load->view('albums/edit_album',$data);
        else:

            $id = $this->input->post('id'); 
            $title = $this->input->post('title');
            $description = $this->input->post('description');
            $path  = $this->session->userdata('image_name_album');
            $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
            $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');


            if ($this->form_validation->run() == FALSE):
                $errors = array();
                $errors =  $this->form_validation->errors_array();
                $data['errors'] = $errors;
                $data['response'] = "Errors";
                echo json_encode($data);
                die();
            else:

                //update($id,$author,$title,$albums,$short,$lang,$status)
                $data['response'] = $this->albums_model->update($id,$title,$description,$path);; 
                $data['query'] = $this->albums_model->get($id); 
                print_r($data); 
                echo json_encode($data);
                die();
            endif;
        endif;



    }
4

1 回答 1

3

您的更新函数中有一个杂散的 db->where ,在它之后没有被调用,因此它被添加到接下来运行的任何查询中。如果它应该遵循 db->set 你需要用括号括起来 {}

 $q1 = $this->db->where('albums');
于 2013-01-30T14:38:19.203 回答