duanhe1976 2018-06-13 14:15
浏览 41
已采纳

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

I am new to Codeigniter and new to the MVC webdesign, i have plant insert form i want to upload image and save image name into database i tried many codes but not works please help me to write upload function

plant insert form is below here

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

My Codeigniter model

       <?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条回答 默认 最新

  • dpn517111 2018-06-13 15:01
    关注

    Hope this will help you :

    Note : make sure you have load database and upload library either in autoload or in controller

    First your form should be like this :

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

    Your controller should be like this :

    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决