如何使用字段代码Codeigniter将图像添加到表单中

时间:2018-06-13 14:15:50

标签: php codeigniter codeigniter-3

我是Codeigniter的新手,也是MVC网页设计的新手,我有植物插入表单我想上传图像并将图像名称保存到数据库中我尝试了很多代码但不起作用请帮我写上传功能

植物插入形式在这里

    <?php echo validation_errors(); ?>
    <?php echo form_open('AddPlant/InsertPlant')?>
        <div class="form-group has-error">
            <label for="name">Name <span class="require">*</span></label>
            <input type="text" class="form-control" name="name" />
        </div>

        <div class="form-group">
            <label for="description">Description</label>
            <textarea rows="5" class="form-control" name="description" > 
        </textarea>
        </div>
        <div class="form-group required">
           <label for="exampleSelect1" class='control-label'>Job Type</label>
           <select class="form-control" id="age" name="age">
            <option value="1">Level 1</option>
            <option value="2">Level 2</option>
            <option value="3">Level 3</option>
           </select>
        </div>
        <div class="form-group">
            <p><span class="require">*</span> - required fields</p>
        </div>
        <div class="form-group">
            <label for="description">Plant Image</label>
            <input type="file" name="plantimg">
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">
                Create
            </button>
            <button class="btn btn-default">
                Cancel
            </button>
        </div>

    <?php echo form_close();?>

我的Codeigniter模型

       <?php
       class Model_plants extends CI_Model
       {

     function insertPlantData(){

    $data =array(

        'name'=> $this->input->post('name',TRUE),
        'description'=> $this->input->post('description',TRUE),
         'age'=> $this->input->post('age',TRUE),

    );
    return $this->db->insert('plants',$data);
}
}

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

注意 :确保您在自动加载或控制器中加载databaseupload < / p>

首先,您的表单应该是这样的:

<?php echo form_open_multipart('AddPlant/InsertPlant')?>
    ............
 <?php echo form_close();?>

你的控制器应该是这样的:

public function InsertPlant()
{
    /*here make sure your path is correct*/
    $config['upload_path']          = FCPATH .'assets/uploads/';
    $config['allowed_types']        = '*';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('plantimg'))
    {
        $error = array('error' => $this->upload->display_errors());
        print_r($error);die;
    }
    else
    {
        //$data = array('upload_data' => $this->upload->data());
        $file_name = $this->upload->data('file_name');
        /*here assuming that your column name for image is image_name, change it not*/
        $data =array(
                'name'=> $this->input->post('name',TRUE),
                'description'=> $this->input->post('description',TRUE),
                'age'=> $this->input->post('age',TRUE),
                'image_name'=> $file_name,
        );
        return $this->db->insert('plants',$data);
    }
}
相关问题