0

I need to insert input type file value into mysql database. I send the file after form validation. But when inserting the value of the file is shown as NULL, and Error Number: 1048 is displays.

I need to insert an image name into the database along with other fields. But the validation of input type File is not working

my code is

controller

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

}
?>

model

<?php

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

}


?>

view

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

I dont really understand the problem. Thanks in advance

2
  • you write $_FILES['book_cover']['name'] in a correct way but after that you try to access with "book_cover"=>$this->input->post("book_cover") ? this doesn't make any sense - i guess even your form_validation is a problem - did you copy this piece of code from somewhere ? take a look at php.net/manual/en/features.file-upload.post-method.php Commented Dec 7, 2018 at 8:48
  • I found the solution, I replace "book_cover"=>$this->input->post("book_cover") this code with "book_cover"=>$_FILES['book_cover']['name'], Now I want to upload this image to a folder Commented Dec 7, 2018 at 11:06

1 Answer 1

1

You cannot just directly insert same name into database you have to store the file type into assets and then get the name of the file and store it into database. try to look at the documentation use file upload library 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];
            }
    }

Now you can store file name into db.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.