1

Currently, I am using Codeigniter 3.1.0. I am trying to insert data using $this->db->insert('table') in Model.

The problem is that when I try to insert a certain filed value as 0, it would not insert 0, instead, it inserts 10.

I have verified the value of the data and its type.

Any suggestion or advice would be appreciated. Thank you in advance.

    $data['Vat'] = 0;

    foreach ( $data as $key => $value ) {
        if ( $value === null ) {
            continue;
        }
        $this->db->set($key, $value);
    }
    // $this->db->set('Vat', 0);

    $this->db->insert(self::TABLE);
    $q = $this->db->affected_rows();
    return $this->db->insert_id();
2
  • Does the table column have a default value, and is the column itself have the same name (and case) as the data key you are using? Commented Feb 28, 2017 at 16:33
  • @gabe3886 I don't have a default value for that column 'Vat'. The name of the column is different from the data key(the name of the column is 'Vat' and the data key is 'vat'). However, I have another columns that are all lower-cased and they dont have any problem with inserting. Commented Mar 2, 2017 at 1:27

2 Answers 2

1

You can have look on your query by using last_query() function. just add below line after $this->db->insert(self::TABLE); and check whether query is perfect or not. You will get idea where you missing.

 echo $this->db->last_query(); die;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. I have tried your suggestion; I copied the query and executed it. It inserts correct value for vat. However, when I try to insert date through model, it inserts 10 instead of 0 which is desired value.
0

The query was correct, however, after put function, update function(which I did not expect) was executed and it updated its value to 10. Thank you all for your responses.

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.