0

我有一个带有文件输入的更新表单,用于添加图片。我想要实现的是,当我将该字段留空时,文件将不会更改。所以我之前上传的照片会保留在那里。

我的表单输入被称为:<td><?= form_upload('aanbiedingfoto');?></td>

我的数据库字段被称为fotonaam.

我尝试了这样的事情,但没有奏效: if($_FILES['aanbiedingfoto']['name'] != '') { $data['fotonaam'] = $_FILES['aanbiedingfoto']['name']; }

我究竟做错了什么?

我的控制器:

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');
}

我的模型:

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);
    }
4

2 回答 2

1

将您的上传代码放在 if 语句中:

    <?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);
    }
    ?>

这是编辑功能

    <?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);
        }
    }
?>
于 2013-06-21T09:06:36.680 回答
0

如果您不上传任何文件,$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);
}

边注:

这是完全可选的。为了提高性能并消除可能的错误,我建议如下:

  • 避免在不需要时加载库或调用方法。
  • 设置函数参数的默认值。

就像一个演示:

控制器:

...
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);
    }
}
...

该模型:

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);
    }
    ...
}
于 2013-06-21T08:54:39.647 回答