A user sees a coupon page, enters the coupon code and if the coupon is valid, they see a page to pick their gift (known as exploits). (Part 1) They can choose any exploit. And after they choose it, it saves it in database after clicking submit. But it only saves it in database if the value already does not exist in database. (Part 2)
So, I have finished Part 1, but Part 2 for me is not working. It saves the value in database every single time, even though the value already exists in database.
Here's my code for my controller:
$result = $this->model_users->insert_used_coupon($exploit, $coupon);
switch ($result) {
case 'used_coupons':
//do stuff
break;
case 'failed':
//do stuff
break;
case 'success':
//do stuff
break;
}
My model file:
$time = date("Y-m-d H:i:s");
$uid = $this->session->userdata('uid');
$this->db->select('uid');
$this->db->where('uid', $uid);
$result = $this->db->get('used_coupons');
if($result->num_rows == 0) {
$data = array(
'coupon' => $exploit,
'exploit' => $coupon,
'time' => $time,
'uid' => $uid);
$this->db->insert('used_coupons', $data);
if ($this->db->affected_rows() === 1) {
return 'success';
} else {
return 'failed';
}
} else {
return 'already_exists';
}
I have no idea why this issue is happening. Any hlep is highly appreciated. Thanks.