Thursday 16 August 2018

Codeigniter : Check if id already exists in database

Check if any id already exists in database table and not exist id, insert in table.



$product_category = $this->input->post('product_category');    // product_category is multiple checkbox name


$dataCatBatch = array();
foreach($product_category as $_category){
$ros = $this->db->get_where('product_category',array('product_id'=>$productId,'category_id'=>$_category))->num_rows();
if($ros == 0){
$dataCatBatch[] = array('product_id'=>(int)$productId,'category_id'=>(int)$_category);
}
}

if(count($dataCatBatch) > 0){
$this->db->insert_batch('product_category',$dataCatBatch);
}

Description : product_category is table name. You can change column name according your table column name. 

No comments:

Post a Comment