how do I send the last insert id data to another function? - php

I want to pass my last insert id from one table into another function when I test with var_dumb() $ids var contains value but after submiting $ids var contains nothing.
can someone show me how to do it in this code,
thank you
this is my controller :
function save()
{
$judul = $this->input->post('judul');
$isi = $this->input->post('isi');
$kategori = implode(',', $this->input->post('kategori'));
$c_date=date("Y-m-d H:i:s");
$data = array(
'judul' => $judul,
'isi' => $isi,
'kategori' => $kategori,
'publish' => $c_date
);
$ids = $this->text_editor_model->simpan($data);
redirect('text_editor');
$last = $this->upload_image($ids);
}
function upload_image($last)
{
$config['upload_path'] = './berkas/news/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = 0;
$this->load->library('upload', $config);
var_dump($last);
if ( !$this->upload->do_upload('file')) {
$this->output->set_header('HTTP/1.0 500 Server Error');
exit;
} else {
$file = $this->upload->data();
$this->output
->set_content_type('application/json', 'utf-8')
->set_output(json_encode(['location' => base_url().'/berkas/news/'.$file['file_name']]))
->_display();
$count = count($file['file_name']);
for ($i=0; $i < $count ; $i++) {
$data2[$i]['id_berita'] = $last;
$data2[$i]['img_name'] = $file['file_name'];
}
$this->text_editor_model->insert_img($data2);
exit;
}
}
my simpan model :
function simpan($data)
{
$this->db->insert('db_news',$data);
$id_berita = $this->db->insert_id();
return $id_berita;
}

if redirect('text_editor'); redirects to another page? then $last = $this->upload_image($ids); will not execute, so you need to call the upload first then redirect
$last = $this->upload_image($ids);
redirect('text_editor');

Related

While inserting multiple images to database more than one row are added in main table which is connected to images table

I'm trying to upload multiple image in codeigniter. For a particular product I want to add the uploaded images to another table calles"image".... while using "lastid" so that I get pid(product id from table product_multi) in images multiples rows are inserted in the product_multi table... the number rows is according to the number of images I upload. Someone help me to fix this.. If I'm not using lastid variable passing it works perfectly/... but I want pid in images tables :(
This is how im getting datable
This is my model
<?php
class ProductmModel extends CI_Model
{
var $PRODUCTNAME='';
var $DESCRIPTION='';
var $CATEGORY='';
var $SUBCAT='';
var $IMAGE='';
public function addproductm()
{
$this->load->database();
$data = array(
"p_name" => $this->PRODUCTNAME,
"p_des" => $this->DESCRIPTION,
"cat" => $this->CATEGORY,
"subcat" => $this->SUBCAT
);
$this->db->insert('product_multi', $data);
$lastid=$this->db->insert_id();
echo "$lastid";
return $lastid;
}
public function addimage()
{
$last=$this->addproductm();
$this->load->database();
$data = array(
"pid" =>$last,
"images" => $this->IMAGE
);
$this->db->insert('image', $data);
}
}
controller code::
public function upload()
{
//image upload
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload',$config);
$this->upload->initialize($config);
$fileInfos = array();
$errors = array();
//uploading
if (! empty($_FILES['images']['name']))
{
$photosCount = count($_FILES['images']['name']);
for ($i = 0; $i < $photosCount; $i ++)
{
$_FILES['image']['name'] = $_FILES['images']['name'][$i];
$_FILES['image']['type'] = $_FILES['images']['type'][$i];
$_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
$_FILES['image']['error'] = $_FILES['images']['error'][$i];
$_FILES['image']['size'] = $_FILES['images']['size'][$i];
if ($this->upload->do_upload('image'))
{
array_push($fileInfos, array(
'fileInfo' => $this->upload->data()
));
$filename =$_FILES['image']['name'] ;
//$filename =$_FILES["name"].$_FILES["type"];
// $data['fileInfos'] = $fileInfo;
$this->load->model("ProductmModel");
$this->ProductmModel->IMAGE = $filename;
}
else
{
array_push($errors, array(
'error' => $this->upload->display_errors()
));
}
}
}
$pname = $this->input->post("name");
$pdes = $this->input->post("pdes");
$caty = $this->input->post("category");
$subcat = $this->input->post("subcat");
$this->ProductmModel->PRODUCTNAME = $pname;
$this->ProductmModel->DESCRIPTION = $pdes;
$this->ProductmModel->CATEGORY = $caty;
$this->ProductmModel->SUBCAT = $subcat;
$this->ProductmModel->addproductm();
}
This is the code after editing as u said...making both insertion in single function
exampleee:
public function upload(){
//image upload
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload',$config);
$this->upload->initialize($config);
$fileInfos = array();
$errors = array();
//uploading
if (! empty($_FILES['images']['name']))
{
$photosCount = count($_FILES['images']['name']);
for ($i = 0; $i < $photosCount; $i ++)
{
$_FILES['images']['name'] = $_FILES['images']['name'][$i];
$_FILES['images']['type'] = $_FILES['images']['type'][$i];
$_FILES['images']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
$_FILES['images']['error'] = $_FILES['images']['error'][$i];
$_FILES['images']['size'] = $_FILES['images']['size'][$i];
if ($this->upload->do_upload('images'))
{
$data = $this->upload->data();
array_push($fileInfos, $data['file_name']);
}
else{
array_push($errors, array(
'error' => $this->upload->display_errors()
));
}
}
}
$product_array = array(
"p_name" => $this->input->post("name"),
"p_des" => $this->input->post("pdes"),
"cat" => $this->input->post("category"),
"subcat" => $this->input->post("subcat")
);
$this->ProductmModel->addproductm($product_array, $fileInfos);
}
Model
public function addproductm($product_array, $fileInfos){
$this->load->database();
$this->db->insert('product_multi', $product_array);
$lastid = $this->db->insert_id();
foreach($fileInfos as $file){
$image_data = array(
"pid" => $lastid,
"images" => $file
);
$this->db->insert('image', $image_data);
}
}

I want stored image delete when Updated with new image

I am new to codeigniter and I am having problem in edit item image, not that it don't get update as it do but there are too many images in the upload folder directory.
What I want to do is I want the previously stored image to be deleted in the upload directory when I update new image.
Here is my controller:
function edit($shop_id = null){
if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
/* Breadcrumbs */
$this->data['breadcrumb'] = $this->breadcrumbs->show();
/* Variables */
$tables = $this->config->item('tables', 'ion_auth');
$this->data['shop_id'] = $shop_id;
/* Get all category */
$this->data['shopInfo'] = $this->shop_model->getShopInfo($shop_id);
//echo "<pre>";print_r( $this->data['shopInfo']);echo "</pre>";exit;
/* Validate form input */
$this->form_validation->set_rules('shop_name', 'Shop Name', 'trim|required');
$this->form_validation->set_rules('shop_latidude', 'Shop Latidude', 'trim|required');
$this->form_validation->set_rules('shop_longitude', 'Shop Longitude', 'trim|required');
if($this->form_validation->run() == true) {
$config['upload_path'] = './assets/uploads/shop/';
//die(var_dump(is_dir($config['upload_path'])));
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = "logo";
$img_upload = $this->upload->do_upload($img);
$data = $this->upload->data();
$file = array('file_name' => $data['file_name'] );
$data = array('upload_data' => $this->upload->data());
$photo = base_url().'assets/uploads/shop/'.$file['file_name'];
if($img_upload == 1 ) $post_photo = $photo;
else $post_photo = $this->input->post('hidden_photo');
if($this->input->post('status')){
$status = 1;
}else{
$status = 0;
}
$shopInfo = array(
'shop_name' => $this->input->post('shop_name'),
'merchant_id' => $this->input->post('merchant_id'),
'photo' => $post_photo,
'description' => $this->input->post('shop_desc'),
'registered_date'=> date('Y-m-d H:i:s'),
'is_active' => 1,
'shop_location' => $this->input->post('shop_loc'),
'shop_address' => $this->input->post('shop_add'),
'shop_phone' => $this->input->post('shop_ph'),
'shop_latitude' => $this->input->post('shop_latidude'),
'shop_longitude'=> $this->input->post('shop_longitude'),
'open_hour' => $this->input->post('open_hour'),
'close_hour' => $this->input->post('close_hour'),
'remark' => $this->input->post('remark'),
'approved' => $status
);
$this->shop_model->shopUpdate($shop_id, $shopInfo);
redirect(base_url() . 'admin/shop', 'refresh');
} else {
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
/* Load Template */
$this->data['merchant'] = $this->merchant_model->getAllMerchants();
$this->template->admin_render("admin/shop/edit", $this->data);
}
}
Here is my Model:
function shopUpdate($shop_id, $shopInfo) {
$this->db->where('shop_id', $shop_id);
if($shopInfo) {
$query = $this->db->update('shop', $shopInfo);
if ($query) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function shopUpdate($shop_id, $shopInfo) {
$q = $this->db->select('photo')->where('shop_id', $shop_id)->get('shop');
if ($q->num_rows() > 0) {
$imgName = $q->row();
// image path must be './admin/shop'
unlink("./{image pathe}/".$imgName);
}
$this->db->where('shop_id', $shop_id);
if($shopInfo) {
$query = $this->db->update('shop', $shopInfo);
if ($query) {
return true;
} else {
return false;
}
} else {
return false;
}
}
you got file name when image got uploaded so get the file name where you are going to update and delete file first. Use unlink to delete file. run update query.
you just need to change in model for updating and deleting at same time.
First, check if new image uploading or not
$new_image = $_FILES['userfile']['name'] ? $_FILES['userfile']['name'] : '';
if($new_image != ''){
$old_image = $this->shop_model->getOlgImage($shop_id);
if(isset($old_image) && file_exists('image-path/photo.jpg')){
unlink('image-path/image');
}
}
Old image is now deleted. Upload new
$config['upload_path'] = './assets/uploads/shop/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = "logo";
Give name of your input type file name to $this->upload->do_upload() not variable
No need to add $this->upload->data() twice
if($this->upload->do_upload('userfile')){
$data = $this->upload->data();
$photo = base_url().'assets/uploads/shop/'.$data['file_name'];
$post_photo = $photo;
} else $post_photo = $this->input->post('hidden_photo');
if($this->input->post('status')){
$status = 1;
}else{
$status = 0;
}
Get old image
public function getOldImage($shop_id){
return $this->db->get_where('shop', ['shop_id' => $shop_id])->row()->photo;
}
First, you need to get image name from the database by ID then update record. Once the record is updated then you can delete that image the folder directory.
function shopUpdate($shop_id, $shopInfo) {
//fetch image name from the database by shop_id
$imageName = $this->db->select('photo')->where('shop_id', $shop_id)->get('shop');
$this->db->where('shop_id', $shop_id);
if($shopInfo) {
$query = $this->db->update('shop', $shopInfo);
if ($query) {
//record is updated successfully now delete that image from folder
if ($imageName->num_rows() > 0) {
unlink("./{image pathe}/".$imageName->row());
}
return true;
} else {
return false;
}
} else {
return false;
}
}
unlink()

Phpexcel replace null value with empty string

I have noticed that if a cell is empty phpexcel gives it a null value. Is there a way i can replace the null value with any empty string before i loop through the array and store into the database. Below is my code and i am using the codeigniter framework.
public function import(){
$id = $this->session->userdata('company_id');
$directoryname = get_company_full_name($id);
if (!is_dir(FCPATH.'assets/customer_documents/Databases/'.$directoryname)) {
mkdir(FCPATH.'assets/customer_documents/Databases/'.$directoryname, 0777, TRUE);
}
$database_name = $this->input->post('database_name');
// $data['addressbook'] = $this->csv_model->get_addressbook($id);
$data['errors'] = ''; //initialize upload error array to empty
$config['upload_path'] = FCPATH.'assets/customer_documents/Databases/'.$directoryname;
$config['allowed_types'] = '*';
$config['max_size'] = '';
$this->upload->initialize($config);
if (!$this->upload->do_upload()) {
$data['errors'] = $this->upload->display_errors();
$data['databases'] = $this->database->get_databases($id);
$this->session->set_flashdata($data);
redirect(base_url().'Databases');
} else {
$file_data = $this->upload->data();
$file_ext = $file_data['file_ext'];
// use custom function to determine if filetype is allowed
if (allow_csv_type($file_ext))
{
$file_path = FCPATH.'assets/customer_documents/Databases/'.$directoryname. '/' .$file_data['file_name'];
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file_path);
$date_uploaded = date("Y-m-d H:i:s");
if ($objPHPExcel) {
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
// array_shift($arr_data); // removes the 1st/header element
//store the database name and details
$database_data = array(
'db_name'=>$database_name,
'company_id' => $id,
'deleted' => 0,
'date_uploaded'=> $date_uploaded
);
$this->database->insert_db($database_data);
$database_id = $this->db->insert_id();
foreach ($arr_data as $row) {
$insert_data = array(
'company_id' => $this->session->userdata('company_id'),
'number'=>$row['A'],
'province'=>$row['B'],
'district'=>$row['C'],
'ward'=>$row['D'],
'farming_type'=>$row['E'],
'commodity'=>$row['F'],
'database_name'=>$database_name,
'db_id' =>$database_id,
'deleted' => 0,
'date_uploaded'=>$date_uploaded
);
$this->database->insert_csv($insert_data, $id);
}
log_message('info', '*****************************Customer with the ID: '.$this->session->userdata('company_id').' and name: '.get_company_full_name($this->session->userdata('company_id')).' uploaded a csv database. The uploder phone number: '.$this->session->userdata('phone').' The database ID: '.$database_id.' The database name: '.$database_name.'*****************************');
$this->session->set_flashdata('success', 'Data Imported Succesfully');
redirect(base_url().'Databases');
} else{
$data['errors'] = "Error occured";
$data['page_class'] = 'Databases-page';
$data['title'] = 'Databases';
$data['content_view'] = 'Databases/index';
$this->template->admin_template($data);
}
}
else {
$this->session->set_flashdata('errors','File type is not allowed!');
redirect('Databases');
}
}
}
}
I am just looking for a way i can add empty sting values on the empty cells that are coming with null values.
Just use a simple array modifier before it reaches PHPExcel.
If performance is an issue, one may consider not to create multiple functions within array_map(). Note that isset() is extremely fast, and this solutions does not call any other functions at all.
$replacements = array(
'search1' => 'replace1',
'search2' => 'replace2',
'search3' => 'replace3'
);
foreach ($a as $key => $value) {
if (isset($replacements[$value])) {
$a[$key] = $replacements[$value];
}
}
See: https://stackoverflow.com/a/32321702/4272537

Codeigniter if id = 1 it works multiple image update but id =2 it's not working

My codes multiple image upload and update mysql db but one problem if id=1 It's working multiple image uploading and update.else It's not working and white page.
tables 2
musteri_soru and musteri_cevap
is updating musteri_cevap in colon resim
controller code:
function duzenle($no)
{
if($_POST)
{
$arr1['baslik'] = $this->input->post('soru');
$this->form_duzenle_model->duzenle($no,$arr1);
if($_FILES){
$dizin= "../upload/form_cevap/";
$dosya_sayi=count($_FILES['cevap']['name']);
for($i=0;$i<=$dosya_sayi;$i++){
$isim= md5(uniqid(rand()));
if(!empty($_FILES['cevap']['name'][$i])){
move_uploaded_file($_FILES['cevap']['tmp_name'][$i],"./$dizin/$isim{$_FILES['cevap']['name'][$i]}");
$arr['resim']= $dizin.$isim.$_FILES['cevap']['name'][$i];
}
$approve[] = $arr['resim'];
$it = $approve;
print_r($approve);
foreach($it as $n => $c):
/* $deneme = $this->form_duzenle_model->cevapDuzenle($n,$c); */
endforeach;
}
}
redirect('form_duzenle/', 'refresh');
}else{
$this->bc->addCrumb('Düzenle');
$veri = $this->form_duzenle_model->form_duzenleGetir($no)->row();
$veri2 = $this->form_duzenle_model->cevapListe($no)->result();
$data = array(
'baslik'=>$veri->baslik,
'veri' =>$veri,
'cevap' =>$veri2
);
$this->bc->addCrumb($veri->baslik,'form_duzenle/duzenle/'.$veri->no);
$this->layout->view('form_duzenle/form_duzenle_duzenle',$data);
}
}
Models code :
function duzenle($no,$data)
{
$this->db->update($this->tablo,$data, array('no' => $no));
}
function cevapDuzenle($n,$dat)
{
$data = array('resim' => $dat);
$this->db->update($this->ctablo,$data, array('soru_no' => $n));
}
My Tables
enter link description here
To be honest, I'm not quite sure how your upload was working as the $_FILES array shouldn't be nesting the uploads in the way you've shown above.
I'm not saying this will definitely work but it should do:
function duzenle($no)
{
//I would possible look at using the Form_validation Library here
if (empty($_POST)) {
$arr1['baslik'] = $this->input->post('soru');
$this->form_duzenle_model->duzenle($no, $arr1);
if (!empty($_FILES)) {
$dizin = "../upload/form_cevap/";
foreach ($_FILES as $name => $file) {
//If there is an error there isn't any reason to try and upload this file
if ($file['error'] !== 0) {
continue;
}
$name = $file['name'];
$isim = md5(uniqid(rand()));
move_uploaded_file($file['tmp_name'], "./$dizin/$isim$name");
$arr['resim'] = $dizin . $isim . $name;
//Not sure what's going on here so I haven't changed it
$approve[] = $arr['resim'];
$it = $approve;
print_r($approve);
foreach ($it as $n => $c):
/* $deneme = $this->form_duzenle_model->cevapDuzenle($n,$c); */
endforeach;
}
}
redirect('form_duzenle/', 'refresh');
}else {
$this->bc->addCrumb('Düzenle');
$veri = $this->form_duzenle_model->form_duzenleGetir($no)->row();
$veri2 = $this->form_duzenle_model->cevapListe($no)->result();
$data = array(
'baslik' => $veri->baslik,
'veri' => $veri,
'cevap' => $veri2
);
$this->bc->addCrumb($veri->baslik, 'form_duzenle/duzenle/' . $veri->no);
$this->layout->view('form_duzenle/form_duzenle_duzenle', $data);
}
}
Hope this helps!
Check your for loop,
$dosya_sayi=count($_FILES['cevap']['name']);
for($i=0;$i<=$dosya_sayi;$i++)
for loop condition should be $i < $dosya_sayi as array index always starts from 0.
So correct for loop is
for($i=0;$i<$dosya_sayi;$i++)
I solved the problem.Duzenle codes on change the bottom code.
function duzenle($no)
{
if($_POST)
{
$arr1['baslik'] = $this->input->post('soru');
$this->form_duzenle_model->duzenle($no,$arr1);
$cevaplar = $this->form_duzenle_model->cevapListe($no)->result_array();
if($_FILES){
$dizin= "../upload/form_cevap/";
foreach($cevaplar AS $cevap){
$i = $cevap['no'];
$isim= md5(uniqid(rand()));
if(!empty($_FILES['cevap']['name'][$i])){
move_uploaded_file($_FILES['cevap']['tmp_name'][$i],"./$dizin/$isim{$_FILES['cevap']['name'][$i]}");
$arr['resim']= $dizin.$isim.$_FILES['cevap']['name'][$i];
}
$approve[] = $arr['resim'];
$it = $approve;
$this->form_duzenle_model->cevapDuzenle($i,$arr['resim']);
}
}
redirect('form_duzenle/', 'refresh');
}else{
$this->bc->addCrumb('Düzenle');
$veri = $this->form_duzenle_model->form_duzenleGetir($no)->row();
$veri2 = $this->form_duzenle_model->cevapListe($no)->result();
$data = array(
'baslik'=>$veri->baslik,
'veri' =>$veri,
'cevap' =>$veri2
);
$this->bc->addCrumb($veri->baslik,'form_duzenle/duzenle/'.$veri->no);
$this->layout->view('form_duzenle/form_duzenle_duzenle',$data);
}
}

How to leave value unchanged if left empty in form Codeigniter

I have an updateform with an file input for adding a picture. What i'm trying to achieve is when I leave that field empty, the file will not be changed. So the photo I've uploaded earlier will stay in there.
My forminput is called: <td><?= form_upload('aanbiedingfoto');?></td>
And my database field is called fotonaam.
I tried something like this, but it did not work:
if($_FILES['aanbiedingfoto']['name'] != '') { $data['fotonaam'] = $_FILES['aanbiedingfoto']['name']; }
What am I doing wrong?
My controller:
function editaanbieding()
{
$data = array(
'Aanbieding' => $this->input->post('aanbiedingnaam'),
'Tekst' => $this->input->post('aanbiedingomschrijving'),
'Prijs' => $this->input->post('aanbiedingprijs'),
'Conditie' => $this->input->post('aanbiedingconditie')
);
if($_FILES['aanbiedingfoto']['name'] != '') { $data['fotonaam'] = $_FILES['aanbiedingfoto']['name']; }
print_r($_FILES);
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $this->input->post('aanbiedingfoto');
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('aanbiedingfoto'))
{
$error = array('error' => $this->upload->display_errors());
}else{
$image_data = $this->upload->data();
}
$this->aanbieding_model->edit_aanbieding($data, $image_data);
redirect('members/aanbiedingen');
}
My model:
function edit_aanbieding($data, $image_data)
{
$id = $this->uri->segment(3);
$id2 = $this->input->post('fotoid');
$id3 = $this->input->post('aanbiedingid');
/*
echo 'bedrijfaanbiedingid ', $id, '<br/>';
echo 'fotoid ', $id2, '<br/>';
echo 'aanbiedingid ', $id3, '<br/>';
die;
*/
$this->db->where('idaanbiedingen', $id3);
$this->db->update('Aanbiedingen', $data);
$this->db->where('idfotoaanbiedingen', $id2);
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->update('fotoaanbiedingen', $insertfoto);
$this->db->where('idbedrijfaanbiedingen', $id);
}
put your upload code inside the if statement:
<?php
if($_FILES['aanbiedingfoto']['name'] != ''){
if ( ! $this->upload->do_upload('aanbiedingfoto')){
$error = array('error' => $this->upload->display_errors());
}else{
$image_data = $this->upload->data();
}
$this->aanbieding_model->edit_aanbieding($data, $image_data, true);
}else{
$this->aanbieding_model->edit_aanbieding($data, $image_data, false);
}
?>
This is the edit function
<?php
function edit_aanbieding($data, $image_data, $uploaded = false){
$id = $this->uri->segment(3);
$id2 = $this->input->post('fotoid');
$id3 = $this->input->post('aanbiedingid');
/*
echo 'bedrijfaanbiedingid ', $id, '<br/>';
echo 'fotoid ', $id2, '<br/>';
echo 'aanbiedingid ', $id3, '<br/>';
die;
*/
$this->db->where('idaanbiedingen', $id3);
$this->db->update('Aanbiedingen', $data);
$this->db->where('idfotoaanbiedingen', $id2);
if($uploaded){ //update photo code
$insertfoto = array('fotonaam' => $image_data['file_name']);
$this->db->where('idbedrijfaanbiedingen', $id);
$this->db->update('fotoaanbiedingen', $insertfoto);
}
}
?>
If you don't upload any file, $image_data would be NULL.
While there are better solutions, you can fix this as simple as:
Find this section in your model and change it as below:
if (isset($image_data)) {
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->where(...); // Fill the blank with correct arguments
$this->db->update('fotoaanbiedingen', $insertfoto);
}
Side note:
This is completely optional. To increase the performance and get rid of probable errors, I suggest the following:
Avoid of loading libraries or calling methods when not needed.
Set default value for function arguments.
Just as a Demo:
The Controller:
...
if (! empty($_FILES['aanbiedingfoto']['name'])) {
$data['fotonaam'] = $_FILES['aanbiedingfoto']['name'];
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $this->input->post('aanbiedingfoto');
$this->load->library('upload', $config);
if ($this->upload->do_upload('aanbiedingfoto')) {
$this->aanbieding_model->edit_aanbieding($data, $this->upload->data());
} else {
$error = array('error' => $this->upload->display_errors());
// If you want to update the table while the uploading failed
$this->aanbieding_model->edit_aanbieding($data);
}
}
...
The Model:
function edit_aanbieding($data, $image_data = NULL)
{
...
if (isset($image_data)) {
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->where(...); // Fill the blank with correct arguments
$this->db->update('fotoaanbiedingen', $insertfoto);
}
...
}

Categories