Reputation: 477
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
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
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
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