更新查詢在mysql [duplicate]中拋出錯誤

[英]update query throws error in mysql [duplicate]


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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/04/72f5d9369d7486875ac937150a00875e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com