Update query throws error in mysql
I have a table named "mostread" with 2 columns open_id (int) and read (int). Now the problem is that the "open_id" is already present in the table, then I need to update the "read" for every mouse click, I need to insert a new line with the "open_id" fetched from the controller and read = 1. I am using the below code in my model that inserts a new row correctly, but the second time I click on it, an error is thrown as follows.
Database error has occurred
Error #: 1064 You have an error in your SQL syntax; check the manual corresponding to your MySQL server version for the correct syntax to use next to 'read = read + 1 WHERE
open_id
=' 193 '' on line 1UPDATE
mostread
SET read = read + 1 WHEREopen_id
= '193'File name: D: /Xampp/htdocs/opunletter/opunletter/application/models/Select.php
Line number: 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;
}
}
source to share