我有这些产品类别的两个表,我存储的产品类别和另一个是我插入产品时插入新产品的产品类别将有一个下拉式选择存储在产品类别中的类别,但是如果您想添加另一个类别,则在下拉列表中会有一个“其他”选项,并且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