我的图像正在上传到另一个文件夹..但图像的名称没有插入数据库中的图像字段名称

时间:2018-01-27 12:46:52

标签: php codeigniter-3

我使用以下代码成功地将表单的所有详细信息插入到数据库中。我的图片也上传到我的上传文件夹,但这些图片名称没有插入数据库图片字段。请告诉我我在哪里做错了。 FYR:我注意到错误发生在数据发布到数据库之前我使用foreach函数进行图像上传,所以那里发生了一些事情

mystruct test;
test.initial();
{
    std::ofstream ofs("filename.dat");
    boost::archive::text_oarchive ar(ofs);
    ar & test;
}
{
    std::ifstream ifs("filename.dat");
    boost::archive::text_iarchive ar(ifs);
    mystruct restored;
    ar & restored;
}
//this is my view code
<div class="col-md-10 leftbar">

<?php if($smsg = $this->session->flashdata('smsg')): ?>
    <div class="alert alert-success dismissible">
        <?= $smsg?>
    </div>
<?php elseif($msg = $this->session->flashdata('msg')):?>
    <div class="alert alert-danger dismissible">
        <?= $msg?>
    </div>
<?php endif;?>

<?php echo form_open_multipart('admin/upro');?>
    <label><h5>product Name:*</h5></label>
        <?php echo form_error('pro_name',"<div class='alert alert-danger'>","</div>");?>
        <?php echo form_input(['name'=>'pro_name','class'=>'form-control','placeholder'=>'product Name Here','value'=>set_value('pro_name')]);?>
    <label><h5>product Category:*</h5></label>
    <select class="form-control" name='pro_cat'>
        <?php foreach ($cat->result() as $c):?>
        <?php echo '<option value="'.$c->cat_name.'">'.ucfirst($c->cat_name).'</option>';?>
        <?php endforeach;?>
    </select><hr>
    <label><h5>product Parent Category:*</h5></label>
    <select class="form-control" name='pro_pa_cat'>
        <?php foreach ($pa_cat->result() as $pc):?>
        <?php echo '<option value="'.$pc->pa_cat_name.'">'.ucfirst($pc->pa_cat_name).'</option>';?>
        <?php endforeach;?>
    </select><hr>
    <label><h5>product Description:*</h5></label>
 <?php echo form_textarea(['name'=>'pro_desc','class'=>'form-control','placeholder'=>'product Description Here','value'=>set_value('pro_desc')]);?>
    <label><h5>product Color:*</h5></label>
<?php echo form_input(['name'=>'pro_color','class'=>'form-control','placeholder'=>'product Color Here','value'=>set_value('pro_color')]);?>
    <label><h5>product Cost:*</h5></label>
<?php echo form_input(['name'=>'pro_cost','class'=>'form-control','placeholder'=>'product Cost Here','value'=>set_value('pro_cost')]);?>
    <label><h5>product Image1:*</h5></label>
<?php echo form_upload(['name'=>'pro_image1']);?>
    <label><h5>product Image2:*</h5></label>
<?php if(isset($upload_error)) echo $upload_error;?>
<?php echo form_upload(['name'=>'pro_image2']);?>
    <label><h5>product Image3:*</h5></label>
<?php if(isset($upload_error)) echo $upload_error;?>
<?php echo form_upload(['name'=>'pro_image3']);?>
    <label><h5>product Image4:*</h5></label>
<?php if(isset($upload_error)) echo $upload_error;?>
<?php echo form_upload(['name'=>'pro_image4']);?>
    <label><h5>product Brand:*</h5></label>
    <select class="form-control" name='pro_brand'>
<?php foreach ($brands->result() as $brand):?>
<?php echo '<option value="'.$brand->brand_name.'">'.$brand->brand_name.'</option>';?>
<?php endforeach;?>
    </select><hr>
    <button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button><hr>
<?php form_close();?>
</div>
//this is my controller vew
public function upro()
{       
    $this->form_validation->set_rules('pro_name','Product','required');
    $this->form_validation->set_rules('pro_cat','Categroy','required');
    $this->form_validation->set_rules('pro_pa_cat','Parent','required');
    $this->form_validation->set_rules('pro_desc','Description','required');
    $this->form_validation->set_rules('pro_color','Color','required');
    $this->form_validation->set_rules('pro_cost','Cost','required');
    $this->form_validation->set_rules('pro_brand','Brand','required');

    $expected_files = array('pro_image1', 'pro_image2', 'pro_image3', 'pro_image4');
    $i = 1;
    foreach ($expected_files as $field_name) {
        if (empty($_FILES[$field_name]['name'])) {
            $this->form_validation->set_rules($field_name, 'Image' . $i, 'required');
        }
        $i++;
    }
    if ($this->form_validation->run()) {
        $config = [
            'upload_path' => './uploads',
            'allowed_types' => 'jpg|gif|png|jpeg',
        ];
        $this->load->library('upload', $config);
        foreach ($_FILES as $field_name => $field_values) {
            if (!in_array($field_name, $expected_files)) {
                continue; // just in case user tries to add more upload fields
            }
            $this->upload->do_upload($field_name);
            $info = $this->upload->data();
            $image_path = $info['raw_name'] . $info['file_ext'];
            $data[$field_values] = $image_path;
        }
        $data= $this->input->post();
        $today = date('Y-m-d');
        $data['pro_date'] = $today;
        echo "<pre>";
        print_r($data);
        $this->adata->uproQ($data);
        $this->session->set_flashdata('smsg', 'Product upload success!');
        return redirect('admin/products');
    } else {
        $this->session->set_flashdata('msg', validation_errors());
        return redirect('admin/apro');
    }
}

1 个答案:

答案 0 :(得分:0)

移动此行:

$data= $this->input->post();

foreach ($_FILES... )之前,$data变量不会被覆盖。

并将$data[$field_values]更改为$data[$field_name],所以它会是这样的:

if ($this->form_validation->run()) {
    ...
    $data = $this->input->post();
    foreach ($_FILES as $field_name => $field_values) {
        if (!in_array($field_name, $expected_files)) {
            continue; // just in case user tries to add more upload fields
        }
        $this->upload->do_upload($field_name);
        $info = $this->upload->data();
        $image_path = $info['raw_name'] . $info['file_ext'];
        $data[$field_name] = $image_path; // change this line from using $field_values
    }
    ...
}