Bakti Wijaya
Bakti Wijaya

Reputation: 477

Insert data to database on condition PHP Codeigniter

How can i inserting data to database using condition ?like the one i'm having now..the condition is..if value is null,then insert data1...else,if value is inserted,then insert data 2...here's is the example of the code..

$insert1 = array(
                'number' => $number,
                'value' => $value,
                'car_id' => $car_id,
                );
$insert2 = array(
                'number' => $number,
                'value' => $value,
                'status' => 1,
                );
    if('car_id'== NULL) <-----(This is the problem)
     {
       $this->db->insert('tbl_a',$insert2);
     }else
     {
       $this->db->insert('tbl_a',$insert1);
     }

what the condition i should put on case like that ? thank you for any helps...

Upvotes: 1

Views: 544

Answers (3)

cnk
cnk

Reputation: 24

$insert1 = array(
                'number' => $number,
                'value' => $value,
                'car_id' => $car_id,
                );
$insert2 = array(
                'number' => $number,
                'value' => $value,
                'status' => 1,
                );
    if($car_id !== NULL) <-----(see here)
     {
       $this->db->insert('tbl_a',$insert1); (change here too )
     }else
     {
       $this->db->insert('tbl_a',$insert2);(change here too )
     }

Upvotes: 0

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Try using isset($insert1['car_id']) to check whether the car_id has value or not.

$insert1 = array(
                'number' => $number,
                'value' => $value,
                'car_id' => $car_id,
                );
$insert2 = array(
                'number' => $number,
                'value' => $value,
                'status' => 1,
                );
    if(isset($insert1['car_id'])) //see here
     {
       $this->db->insert('tbl_a',$insert2);
     }else
     {
       $this->db->insert('tbl_a',$insert1);
     }

Upvotes: 1

Rotimi
Rotimi

Reputation: 4825

You are not doing your null check properly.

Try, If(is_null($car_id) && strlen($car_id) ==0) //insert 1 Else //insert two

Upvotes: 0

Related Questions