简体   繁体   English

使用CodeIgniter更新MySQL表时出错

[英]Error On Updating MySQL table using CodeIgniter

I have a CodeIgniter Application that updates two columns in a table, using the "update()" helper method in codeigniter. 我有一个CodeIgniter应用程序,它使用codeigniter中的“ update()”帮助程序方法更新表中的两列。

This is the controller: 这是控制器:

      $data_v   = array(                
            "status" => $resp->Code
        );
        if($resp->Code == "0") {
            $data_v['name'] = $responseObject->AccountName;
                }
         $this->employees_model->update_employee($id, $data_v);

The model is : 该模型是:

     public function update_employee($id, $data) {

    $id = (int)$id;
    $this->db->where('empID', $id);
    $this->db->update('emp', $data);
}

The issue is that i get the following error: 问题是我得到以下错误:

 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 'NEO WHERE `empID` = 20160'   at line 1


   UPDATE `emp` SET `status` = 0, 
  `name` = ALIBABA, THIEF NEO WHERE `empID` = 20160

My knowledge of SQL points to the fact (i believe) that the character value 我对SQL的了解指出(我相信)字符值这一事实

     ALIBABA, THIEF NEO 

needs to be enclosed in single quotes to look like this 需要用单引号括起来看起来像这样

    'ALIBABA, THIEF NEO'

How can i resolve this? 我该如何解决? and if the issue is not enclosing character values in quotes...what do i do? 如果问题不是用引号引起来的字符值...我该怎么办? Thanks 谢谢

Yes you are right you need to add ' before and after string. 是的,您是对的,您需要在字符串前后添加'

Change from

$data_v['name'] = $responseObject->AccountName;

into 进入

$data_v['name'] = "'".$responseObject->AccountName."'";

You can use escape() method in db class to do it. 您可以在db类中使用escape()方法来执行此操作。 Escape method will find out datatype and add quotes to string. Escape方法将找出数据类型并在字符串中添加引号。

Ex : $data_v['name'] = $this->db->escape($responseObject->AccountName); 例如:$ data_v ['name'] = $ this-> db-> escape($ responseObject-> AccountName);

Note : You can also use $this->db->escape_str(); 注意:您也可以使用$ this-> db-> escape_str();。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM