Updating image in database using file upload

时间:2016-02-03 03:46:35

标签: php database codeigniter

I am trying to update my database through a form which includes updating the stored image in the database but it doesnt work nothing happens if i click the upload button it just redirects me

my view :

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 

        <title>Test</title>




</head>

<body>


<div class="container">


        <div class="row" >                
 <?php echo form_open_multipart('edit_news/update_news'); ?>

            <div class="col-md-10">
                <br>
                <div class="panel panel-default">
                    <div class="panel-body">

                 <?php if (validation_errors()): ?>

                    <div class="alert alert-danger alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <?php echo validation_errors(); ?>
                    </div>

                <?php endif ?>
                <?php foreach ($news as $n): ?>
      <form action="<?php echo base_url() . "edit_news/update_news/". $n->news_id; ?>" method="post" class="form-horizontal" role="form"> 
                    <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Product Image</label>
                                    <div class="col-sm-5">
                                        <input type="file" class="form-control" placeholder="" name="userfile">
                                    </div>

                                </div>
                                <br>
                                <br> <br>
                    <div class="form-group"> <label class="col-sm-2 control-label" ></label>
                        <label class="col-sm-2 control-label" >Product ID:</label>
                        <div class="input-group dis" style="width: 320px;">
                            <input value="<?php echo $n->news_id; ?>" disabled="" type="text" class="form-control " aria-describedby="sizing-addon2" id="id" name="id">
                        </div>
                    </div>
                     <div class="form-group"> <label class="col-sm-2 control-label" ></label>
                        <label class="col-sm-2 control-label" >Product Title:</label>
                        <div class="input-group dis" style="width: 320px;">
                            <input value="<?php echo $n->title; ?>"  type="text" class="form-control " aria-describedby="sizing-addon2" id="name" name="title">
                        </div>
                    </div>
                     <div class="form-group"> <label class="col-sm-2 control-label" ></label>
                        <label class="col-sm-2 control-label" >Product Description:</label>
                        <div class="input-group dis" style="width: 320px;">
                            <input value="<?php echo $n->news_description; ?>"  type="text" class="form-control " aria-describedby="sizing-addon2" id="price" name="description">
                        </div>
                    </div>
                    <?php echo form_error('serial'); ?> 

                     <div class="col-sm-offset-0 col-sm-12"><label class="col-sm-4 control-label" ></label>

                       <button type="button, submit"  class="btn btn-primary " style="border-radius: 0;">
                             Upload
                            </button>
                    </div>


  <?php endforeach; ?>
    <?php echo form_close() ?>
            </div>
        </div>

    </div>
</div>




  </div>
</div>

            </div>

        </div>


                    </div>



                </div>
            </div>


            <script src="<?= base_url(); ?>js/bootstrap.min.js"></script>
            <script>
                $(document).ready(function() {
                    $('#myTable').dataTable();
                });
            </script>
            </body>
            </html>

controller:

public function update_news(){

      $config['upload_path'] = './assets/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200000';
        $config['max_width'] = '200000';
        $config['max_height'] = '200000';
        $config['new_image'] = './assets/';

        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        $this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
        $this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');
        if (!$this->upload->do_upload() || !$this->form_validation->run()) {
            $error = array('error' => $this->upload->display_errors());
            redirect('admin_news_adds');
        } else {
            $data = $this->upload->data();
            $this->thumb($data);
            $id = $this->uri->segment(3);
            $file = array(
                'img_name' => $data['raw_name'],
                'thumb_name' => $data['raw_name'] . '_thumb',
                'ext' => $data['file_ext'],
                'news_date' => date("l , F j, Y "),
                'title' => $this->input->post('title'),
                'status' => 1,
                'news_description' => $this->input->post('description'),
            );
           $this->User->update_news($file,$id);
            $data = array('upload_data' => $this->upload->data());
            redirect('admin_news_add');
        }
    }

model:

public function update_news($file,$id) {
         $this->db->where('news_id',$id);
        $this->db->update('news_table', $file);
    }

3 个答案:

答案 0 :(得分:0)

Maybe any error when you upload the image, you can debug your php source why error happened, like adding var_dump($error) in 1st condition (if (!$this->upload->do_upload() || !$this->form_validation->run()) ) and comment "redirect('admin_news_adds');" just for debugging condition.

public function update_news(){

  $config['upload_path'] = './assets/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '200000';
    $config['max_width'] = '200000';
    $config['max_height'] = '200000';
    $config['new_image'] = './assets/';

    $config['overwrite'] = TRUE;
    $this->load->library('upload', $config);
    $this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
    $this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');
    if (!$this->upload->do_upload() || !$this->form_validation->run()) {
        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
        //redirect('admin_news_adds');
    } else {
        $data = $this->upload->data();
        $this->thumb($data);
        $id = $this->uri->segment(3);
        $file = array(
            'img_name' => $data['raw_name'],
            'thumb_name' => $data['raw_name'] . '_thumb',
            'ext' => $data['file_ext'],
            'news_date' => date("l , F j, Y "),
            'title' => $this->input->post('title'),
            'status' => 1,
            'news_description' => $this->input->post('description'),
        );
       $this->User->update_news($file,$id);
        $data = array('upload_data' => $this->upload->data());
        redirect('admin_news_add');
    }
}

答案 1 :(得分:0)

You have two forms. First when you call form

   <?php echo form_open_multipart('edit_news/update_news'); ?>

And inside foreach block

 <form action="<?php echo base_url() . "edit_news/update_news/". $n->news_id; ?>" 
      method="post" class="form-horizontal" role="form">

which contain Button element. Because uploading need form attribute enctype set to multipart/form-data, your inner form does not have this so no file upload took place.

答案 2 :(得分:0)

试试这个

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

        $config['upload_path'] =  getcwd().'/assets/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200000';
        $config['max_width'] = '200000';
        $config['max_height'] = '200000';
        $config['overwrite'] = TRUE;

        $this->upload->initialize($config);
        $this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
        $this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');

        if (!$this->upload->do_upload() || !$this->form_validation->run()) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('your view name', $error);
        } else {
            $data = $this->upload->data();
            $this->thumb($data);
            $id = $this->uri->segment(3);
            $file = array(
                'img_name' => $data['raw_name'],
                'thumb_name' => $data['raw_name'] . '_thumb',
                'ext' => $data['file_ext'],
                'news_date' => date("l , F j, Y "),
                'title' => $this->input->post('title'),
                'status' => 1,
                'news_description' => $this->input->post('description'),
            );
            $this->User->update_news($file,$id);
            $data = array('upload_data' => $this->upload->data());
            redirect('admin_news_add');
        }
    }