更新查询在mysql [duplicate]中抛出错误

时间:2023-01-31 00:13:32

This question already has an answer here:

这个问题已经有了答案:

I have a table named 'mostread' with 2 columns open_id(int) and read(int). Now the problem is if 'open_id' is already present in the table then i need to update the 'read' for every click else i need to insert a new row with 'open_id' retrieved from the controller and read=1. I am using the below code in my model which inserts a new row properly but the second time i click it throws an error as follows.

我有一个名为“mostread”的表,其中有2列open_id(int)和read(int)。现在的问题是,如果“open_id”已经出现在表中,那么我需要为每一个点击更新“read”,我需要插入一个从控制器检索到的“open_id”的新行,read=1。我正在我的模型中使用下面的代码,它正确地插入了一个新行,但是第二次单击时它会抛出如下所示的错误。

A Database Error Occurred

一个数据库发生错误

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 'read = read+1 WHERE open_id = '193'' at line 1

错误号:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,找到正确的语法,在第1行使用near 'read = read+1,其中open_id = '193 "

UPDATE mostread SET read = read+1 WHERE open_id = '193'

更新mostread SET read = read+1其中open_id = '193'

Filename: D:/Xampp/htdocs/opunletter/opunletter/application/models/Select.php

文件名:D:/ Xampp /根/ opunletter / opunletter /应用程序/模型/ Select.php

Line Number: 52

行号:52

             public function click($id)  
      {  
           $query = $this->db->query("SELECT * FROM mostread WHERE open_id='$id'");  

$count= $query->num_rows();
    if($count > 0) {
        $this->db->set('read', 'read+1', FALSE);
        $this->db->where('open_id', $id);
        $this->db->update('mostread');

        $data = array( 
   'open_id' => $id,
   'read' => '1'
);

$this->db->insert('mostread', $data); 
          return TRUE;
    }else{
         return FALSE;
     }
      }

1 个解决方案

#1


3  

Try adding backticks arround read its a reserved keyword in mysql

试着在mysql中添加回勾读它的保留关键字

$this->db->set('`read`', '`read` + 1', FALSE);

#1


3  

Try adding backticks arround read its a reserved keyword in mysql

试着在mysql中添加回勾读它的保留关键字

$this->db->set('`read`', '`read` + 1', FALSE);