如果不存在则输入Codeigniter,如果不存在则更新 [英] Codeigniter insert if not exist and update if not
问题描述
我有两个表用于这些产品类别,其中i存储产品类别,另一个是在插入新产品时插入产品的产品列表,将有一个下拉列表,可从存储在产品上的类别中进行选择类别,但如果你想添加另一个类别,在下拉列表中有一个其他选项,并且textarea将显示,并允许您创建另一个类别我的问题是,如果我添加一个新的产品与数据库中的现有类别,它不插入两个表,但如果我添加一个新的产品与一个新的类别它成功地插入
我的控制器:
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 query的方法:
There is one more way by using mysql query
$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 |
+----+----------+
示例
已插入到表中,因为没有条目的键 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屋!