首页 > 解决方案 > 带输入名称字段的图像上传

问题描述

我正在使用 Codeigniter 并尝试在数据库中上传带有用户名的图像名称。在我的视图文件中有两个字段,一个是输入字段,第二个是文件类型输入字段。但是当我尝试将第一个输入字段值从一个函数传递到另一个函数时出现问题,它显示为空白。这是我的代码...

控制器

class img_upload extends CI_Controller
{
    public function index()
    {
        $data['title'] = 'Image Upload & Display';
        $this->load->view('img_upload_view',$data);     
    }
    public function check_img()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('userfile', 'Userfile', 'callback_image_validation');

        if($this->form_validation->run() == false)
        {
            $this->index();
        }
        else
        {
            $data['name'] = $this->input->post('name');
            $this->image_validation($data['name']);
        }
    }
    public function image_validation($data)
    {
        $name = $data['name'];
        $config['upload_path']  = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';

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

        if ( ! $this->upload->do_upload('userfile'))
        {
            $error =  $this->upload->display_errors();
            $this->form_validation->set_message('image_validation', $error);
            return false;
        }
        else
        {
            $data = $this->upload->data();
            $img = $data['file_name'];

            $data = array(
                'name' => $name,
                'img_name' => $img
            );

            $this->load->model('img_model');
            $this->img_model->Insert_Data($data);

            $this->session->set_flashdata('success', "Data Inserted Successfully!");

            return true;
        }
    }
}

模型

class img_model extends CI_Model
{
    public function Insert_Data($data)
    {
        $this->db->insert('image', $data);
    }
}

看法

<!DOCTYPE html>
<html>
<body>
    <div class="container">
        <div class="row justify-content-md-center">
            <div class="col-md-8">
                <br>
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title"><h2><?php echo $title; ?></h2></h5>
                        <p class="card-text">
                        <?php if($this->session->flashdata('success') == '')
                        {
                        echo $this->session->flashdata('success');
                        }
                        ?>
                        <?php echo form_open_multipart('img_upload/check_img'); ?>
                        <div class="form-group">
                        <label for="exampleInputName">Name</label>
                        <input type="text" class="form-control" placeholder="Enter username" name="name">
                        <span><?php echo form_error('name');?></span>
                        </div>  
                        <div class="form-group">
                        <label for="exampleInputUpload">Upload</label>
                        <input type="file" class="form-control" name="userfile">
                        <span><?php echo form_error('userfile');?></span>
                        </div>
                        <button type="submit" class="btn btn-primary">Submit</button>
                        </form>
                        <br>
                        <div class="img-reponsive">
                        <!-- <img src="<?php #echo 'upload/'.$images->img_name; ?>" height="150" width="150"> -->  
                        </div>
                        </p>
                    </div>
                </div>

            </div>
        </div>

    </div>
</body>

</html>

错误

   A Database Error Occurred
   Error Number: 1048

   Column 'name' cannot be null

   INSERT INTO `image` (`name`, `img_name`) VALUES (NULL, 'c4.jpg')

   Filename: C:/xampp/htdocs/image_upload/system/database/DB_driver.php

   Line Number: 691

标签: htmlmysqlcodeigniter

解决方案


您不能在控制器更改中将数据作为数组传递

    $data['name'] = $this->input->post('name');
    $this->image_validation($data['name']);

    $name = $this->input->post('name');
    $this->image_validation($name);

尝试这个

public function check_img(){

$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('userfile', 'Userfile', 'callback_image_validation');

if($this->form_validation->run() == false)
{
    $this->index();
}
else
{
    $data['name'] = $this->input->post('name');

    $config['upload_path'] = './upload/';
    $config['allowed_types']        = 'jpeg|jpg|png|gif';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload('image'))
    {
        $error = array('error' => $this->upload->display_errors());
        echo json_encode($error);
    }
    else{ 
        $upload_data = $this->upload->data();
        $data['img_name'] = $upload_data['file_name'];

        $this->load->model('img_model');
        $this->img_model->Insert_Data($data);

        $this->session->set_flashdata('success', "Data Inserted Successfully!");
        return true;
    }
}
}

推荐阅读