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