gpt4 book ai didi

php - Codeigniter 如果不存在则插入,如果不存在则更新

转载 作者:可可西里 更新时间:2023-11-01 12:46:46 25 4
gpt4 key购买 nike

对于这些产品类别,我有两个表,一个用于存储产品类别,另一个是产品列表,当您插入新产品时,我将在其中插入产品,将会有一个下拉菜单供您从存储在产品上的类别中进行选择类别,但如果你想添加另一个类别,下拉列表中有一个“其他”选项,文本区域将出现并让你创建另一个类别我的问题是,如果我添加一个新产品,数据库中有一个现有类别,它不会插入这两个表,但如果我添加一个新类别的新产品,它会成功插入我的 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);
}

最佳答案

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

   $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 已被插入到表中,因为如果我们尝试在位置 1 或 2 上插入数据,那么现在没有带有键 id 的条目

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

关于php - Codeigniter 如果不存在则插入,如果不存在则更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com