代码点火器插入(如果不存在),如果不存在,则更新


Codeigniter insert if not exist and update if not

我为这些产品类别有两个表,一个是我存储产品类别的表格,另一个是产品列表,当您插入新产品时,我将在其中插入产品,将有一个下拉列表可以从存储在产品类别上的类别中进行选择,但是如果您想添加另一个类别,下拉列表中有一个"其他"选项,文本区域将出现并允许您创建另一个类别我的问题是,如果我在数据库中添加具有现有类别的新产品,它不会插入到两个表中,但是如果我添加具有新类别的新产品,它会成功插入我的控制器:

 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);
    }

首先,您需要检查用户或数据是否退出。 然后,您可以执行更新和插入操作。

   $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     |
+----+-------------+

欲了解更多信息

$this->db->replace('profile',$data);

如果存在更新,否则插入