繁体   English   中英

使用文件上传更新数据库中的图像

[英]Updating image in database using file upload

我正在尝试通过一种形式来更新数据库,其中包括更新数据库中存储的图像,但是如果我单击上载按钮,它只会重定向我,那么它什么也不会发生

我的观点 :

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

控制器:

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

模型:

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

上载图片时可能出现任何错误,您可以调试php源,为什么会发生错误,例如在第一种情况下添加var_dump($ error)(如果(!$ this-> upload-> do_upload()||! form_validation-> run()),并注释“ redirect('admin_news_adds');” 仅用于调试条件。

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

您有两种形式。 首先,当您致电表格

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

在foreach块里面

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

其中包含Button元素。 由于上载需要将表单属性enctype设置为multipart / form-data,因此您的内部表单没有此属性,因此没有文件上传发生。

尝试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM