php - Codeigniter - Checking if value already exists in database with $result->num_rows == 0 not working -
a user sees coupon page, enters coupon code , if coupon valid, see page pick gift (known exploits). (part 1) can choose exploit. , after choose it, saves in database after clicking submit. but saves in database if value not exist in database. (part 2)
so, have finished part 1, part 2 me not working. saves value in database every single time, though value exists in database.
here's code 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 issue happening. hlep highly appreciated. thanks.
you try replacing
if($result->num_rows == 0) {
with
if($result->num_rows() > 0) {
or
if($result->num_rows() == 0) {
Comments
Post a Comment