Image Upload With Input Name Field

问题: I am using Codeigniter and trying to upload image name with username in database. In my view file there are two fields one input field and second one file type input field....

问题:

I am using Codeigniter and trying to upload image name with username in database. In my view file there are two fields one input field and second one file type input field. But there is problem when I am trying to pass first input field value from one function to another its shows blank. here is my code...

controller

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

model

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

view

<!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>

error

   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

回答1:

You can't pass data as array in the controller change

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

to

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

Try this

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

回答2:

There is no need to go to another function image_validation(). You can insert data in the same else clause as below.

public function check_img()
{
    $this->load->library('form_validation');

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

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

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

        if( $_FILES['userfile']['name']!='' && $_FILES['userfile']['size'] > 0 )
        {
            //NOTE :- To give uploaded image your Input field name, pass the $name variable to $config['file_name'] name.

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

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

            if( $this->upload->do_upload('userfile') )
            {
                $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;
            }
            else
            {
                $error =  $this->upload->display_errors();
                $this->form_validation->set_message('image_validation', $error);
                return false;
            }
        }
        else
        {
            //show error to select Image.
        }
    }
}
  • 发表于 2019-02-14 01:00
  • 阅读 ( 166 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除