Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

php - Codeigniter insert if not exist and update if not

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

model

public function insert_prod($file){
        $this->db->insert('product_table',$file);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

now we are performing ON DUPLICATE KEY UPDATE

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

result is

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

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';

Then result is

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

For more information


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...