2016-02-05 128 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

相关问题