【发布时间】:2015-10-26 21:07:28
【问题描述】:
我有一个名为“mostread”的表,其中包含 2 列 open_id(int) 和 read(int)。现在的问题是,如果表中已经存在“open_id”,那么我需要为每次点击更新“读取”,否则我需要插入一个新行,其中包含从控制器检索到的“open_id”并读取 = 1。我在我的模型中使用下面的代码,它正确地插入了一个新行,但是我第二次单击它时会引发如下错误。
发生数据库错误
错误号:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'read = read+1 WHERE
open_id= '193'' 附近使用正确的语法更新
mostreadSET read = read+1 WHEREopen_id= '193'文件名:D:/Xampp/htdocs/opunletter/opunletter/application/models/Select.php
行号: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;
}
}
【问题讨论】:
-
read是保留字,用反引号括起来dev.mysql.com/doc/refman/5.5/en/keywords.html
标签: php mysql codeigniter mysqli