Codeigniter 如果不存在则插入,如果不存在则更新 [英] Codeigniter insert if not exist and update if not

查看:47
本文介绍了Codeigniter 如果不存在则插入,如果不存在则更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

推荐答案

首先需要检查用户或数据是否存在.然后就可以进行更新和插入操作了.

First you need to check whether the user or data is exits or not. then you can perform the update and insert operation.

   $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 查询

There is one more way by using mysql query

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

说明假设这是表.

Explanation Suppose this is the table.

+----+----------+
| 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 上插入数据,那么现在是否存在

Example has been inserted into the table because there is no entry with the key id is there now if we are trying to insert data on the position 1 or 2 then

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

那么结果是

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

更多信息

这篇关于Codeigniter 如果不存在则插入,如果不存在则更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆