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

时间:2023-01-18 21:21:17

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 个解决方案

#1


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.

上传图片时可能有任何错误,你可以调试你的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');
    }
}

#2


0  

You have two forms. First when you call form

你有两种形式。首先,当你打电话给表格

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

And inside foreach block

并且在foreach区域内

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

其中包含Button元素。因为上传需要将表单属性enctype设置为multipart / form-data,所以你的内部表单没有这个,所以没有发生文件上传。

#3


0  

Try this

尝试这个

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

#1


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.

上传图片时可能有任何错误,你可以调试你的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');
    }
}

#2


0  

You have two forms. First when you call form

你有两种形式。首先,当你打电话给表格

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

And inside foreach block

并且在foreach区域内

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

其中包含Button元素。因为上传需要将表单属性enctype设置为multipart / form-data,所以你的内部表单没有这个,所以没有发生文件上传。

#3


0  

Try this

尝试这个

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