2016-02-05 123 views
5

我有這些產品類別的兩個表,我存儲的產品類別和另一個是我插入產品時插入新產品的產品類別將有一個下拉式選擇存儲在產品類別中的類別,但是如果您想添加另一個類別,則在下拉列表中會有一個「其他」選項,並且textarea將會顯示並且可以創建另一個類別。我的問題是如果我添加新產品現有類別中它不插入到兩個表的數據庫,但如果我添加一個新的產品,新的類別是successfuly插入 我的控制器:Codeigniter插入如果不存在和更新,如果不是

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->load->library('upload', $config); 
    $this->form_validation->set_rules('name', 'Product Name', 'required|xss_clean'); 
    $this->form_validation->set_rules('description', 'Product Description', 'required|xss_clean'); 
    $this->form_validation->set_rules('price', 'Price', 'required'); 
    if (!$this->upload->do_upload() || !$this->form_validation->run()) { 
     $error = array('error' => $this->upload->display_errors()); 
     redirect('add_products'); 
    } else { 

     $data = $this->upload->data(); 
     $this->thumb($data); 

     $category = $_POST["prod_category"]; 
     if($category == "2") 
      { 
      $category = $_POST["other_category"]; 

      } 
     $file = array(
      'img_name' => $data['raw_name'], 
      'thumb_name' => $data['raw_name'] . '_thumb', 
      'ext' => $data['file_ext'], 
      'product_name' => $this->input->post('name'), 
      'product_description' => $this->input->post('description'), 
      'product_price' => $this->input->post('price'), 
      'product_category' =>$category,  


     ); 

     $this->db->insert("product_category",array("category"=>$category)); 
      $this->User->insert_prod($file); 
     $data = array('upload_data' => $this->upload->data()); 
     echo '<script>alert("You Have Successfully Added a new Product!");</script>'; 
     redirect('admin_products','refresh'); 
    } 
} 

模型

public function insert_prod($file){ 
     $this->db->insert('product_table',$file); 
    } 
+0

不知道是否有資格作爲一個答案:http://andrea.codes/codeigniter-if-record-exists-then-update-if -not-insert/ 它太冗長的模式... – SparK

+0

嘗試使用它,但似乎無法使其工作 – Christian

回答

9

首先您需要檢查用戶或數據是否退出。那麼你可以執行更新和插入操作。

$this->db->where('user_id',$id); 
    $q = $this->db->get('profile'); 

    if ($q->num_rows() > 0) 
    { 
     $this->db->where('user_id',$id); 
     $this->db->update('profile',$data); 
    } else { 
     $this->db->set('user_id', $id); 
     $this->db->insert('profile',$data); 
    } 

有使用MySQL查詢

$query = 'INSERT INTO table_name (id, name) VALUES (?, ?) 
    ON DUPLICATE KEY UPDATE name=VALUES(name)'; 

說明 假設這是表中的一個多方式。

+----+----------+ 
| id | name  | 
+----+----------+ 
| 1 | Urfusion | 
| 2 | Christian| 
+----+----------+ 

我們現在正在執行ON DUPLICATE KEY UPDATE

INSERT INTO table_name VALUES (3,'Example') ON DUPLICATE KEY UPDATE name='Example'; 

結果是

+----+----------+ 
| id | name  | 
+----+----------+ 
| 1 | Urfusion | 
| 2 | Christian| 
| 3 | Example | 
+----+----------+ 

Example已經被插入到表中,因爲沒有與關鍵id沒有條目存在,如果現在我們試圖在位置1或2上插入數據,然後

INSERT INTO table_name VALUES (1,'Name_changed') ON DUPLICATE KEY UPDATE name='Name_changed'; 

那麼結果是

| id | name  | 
+----+-------------+ 
| 1 | Name_changed| 
| 2 | Christian | 
| 3 | Example  | 
+----+-------------+ 

更多information

+0

即時通訊不是很熟悉第二個選項,你能解釋更多?我已經閱讀了你給出的鏈接,但它讓我困惑,第一個選項對我無效 – Christian

+0

@Christian:檢查更新後的答案。 – urfusion

+0

哦,我看到了,但我有點需要幫助實現這一點,我試圖使用它,但它不工作,所以我只是把查詢或我需要別的東西的例子這是我的表 $查詢='INSERT INTO product_category(id,category) VALUES(?,?) ON DUPLICATE KEY UPDATE category = VALUES(category)'; 就是這樣嗎? – Christian

相關問題