【问题标题】:Updating image in database using file upload使用文件上传更新数据库中的图像
【发布时间】:2016-02-03 03:46:35
【问题描述】:

我的看法:

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


    【解决方案1】:

    上传图片时可能出现任何错误,您可以调试您的 php 源代码为什么会发生错误,例如在第一个条件中添加 var_dump($error) (if (!$this->upload->do_upload() || !$ this->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');
        }
    }
    

    【讨论】:

    • 我没有收到任何错误,尽管我尝试手动将模型中的 id 设置为类似这样的东西 public function update_news($file) { $this->db->where('news_id','1 '); $this->db->update('news_table', $file);并且它起作用了,所以我猜问题出在 id 上?
    • 请尝试将 '$id = $this->uri->segment(3)' 更改为 '$id =$this->input->post('id');'那么。
    【解决方案2】:

    你有两种形式。首先当你调用表单时

       <?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,所以你的内部表单没有这个,所以没有进行文件上传。

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 2022-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多