title: Codeigniter insert if not exist and update if not
body: <p>i have two tables for these product category the one  where i store the product categories and the other one is the product list where i insert products when you insert a new product there will be a dropdown to choose from categories stored on the product category but if you want to add another category there is an "other" option in the dropdown and a textarea will apear and lets you create another category my problem is if I add a new product with an existing category in the database it doesnt insert into the two tables but if I add a new product with a new category it successfuly inserts 
  my controller:</p>
  
  <pre><code> function do_upload() {
  
  
      $config['upload_path'] = './assets/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '2000';
      $config['max_width'] = '2000';
      $config['max_height'] = '2000';
      $config['new_image'] = './assets/';
  
      $config['overwrite'] = TRUE;
      $this-&gt;load-&gt;library('upload', $config);
      $this-&gt;form_validation-&gt;set_rules('name', 'Product Name', 'required|xss_clean');
      $this-&gt;form_validation-&gt;set_rules('description', 'Product Description', 'required|xss_clean');
      $this-&gt;form_validation-&gt;set_rules('price', 'Price', 'required');
      if (!$this-&gt;upload-&gt;do_upload() || !$this-&gt;form_validation-&gt;run()) {
          $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
          redirect('add_products');
      } else {
  
          $data = $this-&gt;upload-&gt;data();
          $this-&gt;thumb($data);
  
           $category = $_POST["prod_category"];
          if($category  == "2")
              {
              $category = $_POST["other_category"];
  
              }
          $file = array(
              'img_name' =&gt; $data['raw_name'],
              'thumb_name' =&gt; $data['raw_name'] . '_thumb',
              'ext' =&gt; $data['file_ext'],
              'product_name' =&gt; $this-&gt;input-&gt;post('name'),
              'product_description' =&gt; $this-&gt;input-&gt;post('description'),
              'product_price' =&gt; $this-&gt;input-&gt;post('price'),
              'product_category' =&gt;$category,    
  
  
          );
  
           $this-&gt;db-&gt;insert("product_category",array("category"=&gt;$category));
            $this-&gt;User-&gt;insert_prod($file);
          $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
          echo '&lt;script&gt;alert("You Have Successfully Added a new Product!");&lt;/script&gt;';
          redirect('admin_products','refresh');
      }
  }
  </code></pre>
  
  <p>model</p>
  
  <pre><code>public function insert_prod($file){
          $this-&gt;db-&gt;insert('product_table',$file);
      }
  </code></pre>