I have something like voting database, so I made my database like this Vote_table:
user_id
product_id
vote
primary key is both user_id & product_id
so that no one can vote for a product twice, and this is my model:
public function add_vote_records($product_id, $user_id)
{
$this->db->set('product_id', $product_id);
$this->db->set('members_id', $user_id);
$query = $this->db->insert('product_votes');
if ($query->num_rows() == 0)
return FALSE;
return $query->result_array();
}
and this is my controller:
if ($this->products_model->add_vote_records($product_id, $user_id)) {
$this->product($product_id);
} else {
$this->session->set_flashdata('flash_message', 'Sorry you already voted.');
$this->product($product_id);
}
but it gives me this error when im trying to vote when I already voted:
Duplicate entry '8-2' for key 'PRIMARY'
And that's what I really want, but I want to give the user another message my own message instead of this error.