将输入类型文件值插入mysql数据库

时间:2018-12-07 08:20:19

标签: php codeigniter

我需要将输入类型文件值插入mysql数据库。我在表单验证后发送文件。但是在插入文件时,该值显示为NULL,并显示错误号:1048。

我需要将图像名称和其他字段一起插入数据库。但是输入类型File的验证不起作用

我的代码是

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Book extends CI_Controller {

    public function index()
    {
        $this->load->view('admin/add_book');
    } 

    public function addbook()
    {
        $this->load->view('admin/add_book');
    } 

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

        $this->form_validation->set_rules("book_name","Book Name",'required');
        $this->form_validation->set_rules("book_author","Author",'required');
        $this->form_validation->set_rules("book_pub","Publications",'required');
        $this->form_validation->set_rules("book_genre","Genre",'required|alpha');
        $this->form_validation->set_rules("pub_year","Year",'required');

        if (empty($_FILES['book_cover']['name']))
        {
            $this->form_validation->set_rules("book_cover","Document",'required');
        }

        if($this->form_validation->run())
        {
             $this->load->model("book_model");
             $data=array(
                "book_name"=>$this->input->post("book_name"),
                "book_author"=>$this->input->post("book_author"),
                "book_publication"=>$this->input->post("book_pub"),
                "book_genre"=>$this->input->post("book_genre"),
                "book_year"=>$this->input->post("pub_year"),
                "book_cover"=>$this->input->post("book_cover")
             );
                $this->book_model->insert_data($data);
                redirect(base_url()."admin/book/inserted");
            echo "Success";
        }
        else
        {
            $this->index();
            //echo "Fail";
        }

    }

    public function inserted()
    {
        $this->addbook();
    } 

}
?>

模型

<?php

class Book_model extends CI_Model
{
    function insert_data($data)
    {
        $this->db->insert("books",$data);
    }

}


?>

视图

<form action="<?php echo base_url()?>admin/book/form_validation" method="post" class="form-horizontal form-bordered" enctype="multipart/form-data" >


                                    <?php 
                                    if($this->uri->segment(3) == "inserted")
                                    {
                                        echo '<p class="text-success" style="text-align:center;">Data Inserted</p>';
                                    }

                                    ?>


                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-email">Book Name</label>
                                        <div class="col-md-9">
                                            <input type="text"  name="book_name" class="form-control" placeholder="Enter Name..">
                                            <span class="text-danger"><?php echo form_error("book_name");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-password">Author Name</label>
                                        <div class="col-md-9">
                                            <input type="text" name="book_author" class="form-control" placeholder="Enter Author..">
                                            <span class="text-danger"><?php echo form_error("book_author");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-password">Publications</label>
                                        <div class="col-md-9">
                                            <input type="text" name="book_pub" class="form-control" placeholder="Enter Publication..">
                                            <span class="text-danger"><?php echo form_error("book_pub");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-password">Genre</label>
                                        <div class="col-md-9">
                                            <input type="text" name="book_genre" class="form-control" placeholder="Enter Genre..">
                                            <span class="text-danger"><?php echo form_error("book_genre");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-password">Year</label>
                                        <div class="col-md-9">
                                            <input type="text" name="pub_year" class="form-control" placeholder="Enter Salary..">
                                            <span class="text-danger"><?php echo form_error("pub_year");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="example-hf-password">Book Cover Image</label>
                                        <div class="col-md-9">
                                            <input type="file" name="book_cover" class="form-control" placeholder="upload book cover..">
                                            <span class="text-danger"><?php echo form_error("book_cover");?></span>
                                        </div>
                                    </div>
                                    <div class="form-group form-actions">
                                        <div class="col-md-9 col-md-offset-3">
                                            <button type="submit" name="save" class="btn btn-sm btn-primary"><i class="fa fa-user"></i> Save</button>
                                            <button type="reset" class="btn btn-sm btn-warning"><i class="fa fa-repeat"></i> Reset</button>
                                        </div>
                                    </div>
                                </form>

我不太了解这个问题。 预先感谢

1 个答案:

答案 0 :(得分:0)

您不能只将相同的名称直接插入数据库中,而必须将文件类型存储到资产中,然后获取文件名并将其存储到数据库中。 尝试查看文档使用文件上传库https://www.codeigniter.com/userguide3/libraries/file_uploading.html

public function do_upload()
    {
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

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

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $file_name=$data['upload_data'][file_name];
            }
    }

现在您可以将文件名存储到db中。