3

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.

2 Answers 2

1

You could try replacing

if($result->num_rows == 0) {

with

if($result->num_rows() > 0) {

or

if($result->num_rows() == 0) {

Sign up to request clarification or add additional context in comments.

1 Comment

if($result->num_rows() > 0) { solved it. thank you so much!
1

num_rows is a function, not a property.

if($result->num_rows() == 0) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.