我有這些產品類別的兩個表,我存儲的產品類別和另一個是我插入產品時插入新產品的產品類別將有一個下拉式選擇存儲在產品類別中的類別,但是如果您想添加另一個類別,則在下拉列表中會有一個「其他」選項,並且textarea將會顯示並且可以創建另一個類別。我的問題是如果我添加新產品現有類別中它不插入到兩個表的數據庫,但如果我添加一個新的產品,新的類別是successfuly插入 我的控制器:Codeigniter插入如果不存在和更新,如果不是
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);
}
不知道是否有資格作爲一個答案:http://andrea.codes/codeigniter-if-record-exists-then-update-if -not-insert/ 它太冗長的模式... – SparK
嘗試使用它,但似乎無法使其工作 – Christian