簡體   English   中英

為什么這個MySQL INSERT不能在CI中工作?

[英]Why is this MySQL INSERT not working in CI?

由於某種原因,此SQL語句無效。 誰能告訴我為什么? (這是一個Codeigniter網站,如果這很重要)

這是我的模型(我的錯誤指向我)

public function edit_profile($ID, $field, $new_info)
{
    $sql = "UPDATE users SET ?=? WHERE id=?";
    $query = $this->db->query($sql, array($field, $new_info, $ID)); // <<<< LINE 42
    return $query;
}

這就是我得到的錯誤

Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''first_name'='oooo' WHERE id='151'' at line 1
UPDATE users SET 'first_name'='oooo' WHERE id='151'
Filename: /Applications/MAMP/htdocs/MY_SITE/models/member_model.php
Line Number: 42

我的表名為'users',我有一個'first_name'和'id'列。

想法?

編輯

僅僅因為它似乎有點出現我想澄清我在這里傳遞的變量沒有引號或背景。 它們被添加到某個地方(它看起來像 - >查詢方法,但我想不到這是真的嗎?不過,但是,因為它是我的第一個CI項目)

這是傳遞給模型的控制器......

public function profileEdit()
{
    $ID = $this->the_user->id;
    $field = $this->input->post('edit_field')
    $field = strstr($field,'_edit', true);
    $new_info = $this->input->post('new_info');

    $this->load->model('Member_model');
    if( $this->Member_model->edit_profile( $ID, $field, $new_info )){
        echo 'success';
    }
    else
    {
        echo 'error';
    }
}

我建議使用這樣的:

public function edit_profile($ID, $field, $new_info)
{
    $sql = "UPDATE users SET $field =? WHERE id=?"; # UPDATED (remove $this->db->escape())
    $query = $this->db->query($sql, array($new_info, $ID)); // <<<< LINE 42
    return $query;
}

如果$ field不安全,您可以使用轉義函數。
編輯:
$ this-> db-> escape()將在變量周圍添加引號,這樣你就會再次出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM