I have a model in my codeigniter application that adds a point to an entry every time a user clicks a button.
The points are stored in the "Items" table in my database as a field. points int(11). The points is set as default 0 in the database.
The code that adds a value of 1 to the current value of points in the table for an item is shown below:
public function add_point($id, $current)
{
$data = array(
'points' => $current+1,
);
$this->db->where('item_id', $id);
$this->db->update('Items', $data);
}
This only works the first time adding a point. When I click the button, when the current point on the item is 0 (the default), it adds the point and the database shows the value changed from 0 to 1. But when I try again, and click the button, and it should add and update the value from 1 to 2, it doesn't add the point.
The code that gets the current value and passes it to my model is in my controller:
$getq=$this->upload_model->get_item($item_id);
foreach ($getq as $item) {
$item['points'];
}
$this->upload_model->add_points($item_id,$item['points']);
However it will always update from 0 to 1, and add that point only.
Not sure what I'm doing wrong. How do I fix this so that the current value in the database is incremented by 1 every time the button is clicked.