编辑页面更新问题->覆盖的内容超出应有的范围

发布于 2024-11-06 12:11:51 字数 11625 浏览 1 评论 0原文

我的编辑页面遇到一个非常奇怪的问题:

如果我有超过一次销售:

如果我有一次销售并上传新图片 ->它保存文件名和缩略图名称,但在服务器上不保存图像。

如果我不上传图片 ->它会用最后编辑的销售覆盖所有其他销售的内容,但保留正确的图像。

如果我上传图像,它会删除这两个图像,并且仍然会覆盖上次编辑的销售内容

我已在 MAMP 本地服务器和 cPanel 服务器上尝试过此操作

print_r

正常

Array ( [0] => Array ( [id] => 62 [name] => The Dream [location] => Otara [bedrooms] => 1 [bathrooms] => 1 [condition] => OK [description] =>
What a nice house

[price] => 30.00000 [imagename] => Shack.jpg [thumbname] => Shack_thumb.jpg ) )

编辑

Array ( [0] => Array ( [id] => 62 [name] => The Dream [location] => Otara [bedrooms] => 1 [bathrooms] => 5 [condition] => OK [description] =>
What a nice house

[price] => 30.00000 [imagename] => Mutton.jpg [thumbname] => Mutton_thumb.jpg ) ) 

视图

<?php
//Setting form attributes
$formEditSale = array('id' => 'editSale', 'name' => 'editSale');
$formEditSaleName = array('id' => 'name', 'name' => 'name','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleLocation = array('id' => 'location', 'name' => 'location','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleBedrooms = array('id' => 'bedrooms','name' => 'bedrooms','class' => 'validate[required[custom[number]]]');
$formEditSaleBathrooms = array('id' => 'bathrooms','name' => 'bathrooms','class' => 'validate[required[custom[number]]]');
$formEditSaleCondition = array('id' => 'condition','name' => 'condition','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleImage = array('id' => 'userfile', 'name'=> 'userfile');
$formEditSalePrice = array('id' => 'price','name' => 'price','class' => 'validate[required[custom[number]]]');
$formEditSaleDescription = array('id' => 'description','name' => 'description','class' => '');
$formEditSaleSubmit = array('id' => 'submit', 'name'=> 'submit', 'value' => 'Submit Edit');
?>

<div id ="formLayout" class="form">
    <?php
    if($success == TRUE) {
    echo '<section id = "validation">Sale Updated</section>';
    }

    echo '<section id = "validation">'.$message['imageError'].'</section>';
    ?>
<h4>You are editing sale: <?= $sale[0]['name']; ?></h4>
<?php echo form_open_multipart('admin/editsale/index/'.$sale[0]['id'].'/'.url_title($sale[0]['name'],'dash', TRUE),$formEditSale); ?>
<?php echo form_fieldset(); ?>

<?php echo form_label('Name:','name'); ?>
<?php echo form_input($formEditSaleName, $sale[0]['name']); ?>
<div id="errorName"><?php echo form_error('name'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Location:','location');?> 
<?php echo form_input($formEditSaleLocation, $sale[0]['location']);?>
<div id="errorLocation"><?php echo form_error('location'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Bedrooms: ','bedrooms');?>
<?php echo form_input($formEditSaleBedrooms, $sale[0]['bedrooms']);?>
<div id="errorBedrooms"><?php echo form_error('bedrooms'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Bathrooms: ','bathrooms');?>
<?php echo form_input($formEditSaleBathrooms, $sale[0]['bathrooms']);?>
<div id="errorBathrooms"><?php echo form_error('bathrooms'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Condition: ','condition');?>
<?php echo form_input($formEditSaleCondition, $sale[0]['condition']);?>
<div id="errorCondition"><?php echo form_error('condition'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Price: ','price');?>
<?php echo form_input($formEditSalePrice, $sale[0]['price']);?>
<div id="errorPrice"><?php echo form_error('price'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Image: ','userfile');?>
<?php echo form_upload($formEditSaleImage);?>
<div id="errorUserfile"><?php echo form_error('userfile'); ?></div>
<span class="small">1MB Max JPG/PNG</span>

<?php echo form_label('Description: ','description');?>
<div id="errorDescription"><?php echo form_error('description'); ?></div>
 <span class="small">Required Field - Text</span>
<?php echo form_textarea($formEditSaleDescription, $sale[0]['description']);?>
<script type="text/javascript">CKEDITOR.replace('description');</script>

<?php echo form_submit($formEditSaleSubmit);?>
<?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>
</div>

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Editsale extends CI_Controller {

        function __construct() {
        parent::__construct();

        }

        function index() {
           if(!$this->session->userdata('logged_in'))redirect('admin/home');

        $id = $this->uri->segment(4);
        # Set Main Page Data
            $data['title'] = 'Edit Sale:';
            $data['sales_pages'] = $this->sales_model->getSalesPages();
            $data['cms_pages'] = $this->navigation_model->getCMSPages();
            $data['sale']= $this->sales_model->getSalesContent($id);

             #Set The Validation Rules
                    $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('location', 'Location', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|numeric|required|xss_clean');
                    $this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|numeric|required|xss_clean');
                    $this->form_validation->set_rules('condition', 'Condition', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('price', 'Price', 'trim|required|xss_clean');

                    if($this->form_validation->run()) #If Valid Run
                    {
                        $content = array(   
                            'name' => $this->input->post('name', TRUE), 
                            'location' => $this->input->post('location', TRUE), 
                            'bedrooms' => $this->input->post('bedrooms', TRUE), 
                            'bathrooms' => $this->input->post('bathrooms', TRUE), 
                            'condition' => $this->input->post('condition', TRUE), 
                            'description' => $this->input->post('description', TRUE), 
                            'price' => $this->input->post('price', TRUE)
                        );

                        if($_FILES['userfile']['error'] != 4) 
                        {  
                            //Set File Settings 
                              $config['upload_path'] = 'includes/uploads/gallery/'; 
                              $config['allowed_types'] = 'jpg|png'; 
                              $config['remove_spaces'] = TRUE;
                              $config['max_size'] = '1024'; 
                              $config['max_width'] = '1024'; 
                              $config['max_height'] = '768';

                            $this->load->library('upload', $config);
                            //Continue and resize the image
                            $data = array('upload_data' => $this->upload->data());
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;          #resize
                            $config['new_image'] = 'includes/uploads/gallery/thumbs/';
                            $config['create_thumb'] = 'TRUE';
                            $config['thumb_marker'] ='_thumb';
                            $config['maintain_ratio'] = 'FALSE';
                            $config['width'] = '175';
                            $config['height'] = '175';

                            $this->load->library('image_lib', $config);
                            $this->image_lib->resize(); 


                            if(!$this->upload->do_upload()) //Process the upload function and uploads image
                            {
                                $data['message'] = array('imageError' => $this->upload->display_errors()); // Capture any $config errors and output them
                            }
                            else
                            {
                                $file_info = $this->upload->data(); //Returns an data array 

                                // Append image data to content array in preparation for db update
                                $content['imagename'] = $file_info['file_name'];
                                $content['thumbname'] = $file_info['raw_name'].'_thumb'.$file_info['file_ext'];
                            }   
                        }   

                        // After everything, update DB

                        if($this->sales_model->updateSale($id, $content))
                        {
                            $data['success'] = TRUE;
                        }
                        else
                        {
                            $data['success'] = FALSE;
                        }

                 } # End Form Validation
            $data['cms_pages'] = $this->navigation_model->getCMSPages();
            $data['sales_pages'] = $this->sales_model->getSalesPages($id);
            $data['sale']= $this->sales_model->getSalesContent($id);
            $data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
            $this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it   
        } # End Index Function
    } # End Controller

模型

function updateSale($id, $content) { //Content id from being passed
    $this->db->where('id', $id);  // selecting the $id to update
    $update = $this->db->get('sales'); // What does $update = well it = get the db sales
    $row = $update->row_array(); // what does $row mean = well it gets the row as an array

    if($update->num_rows() > 0) {

        if(isset($content['imagename']) && isset($content['thumbname'])) {

        #lets delete the image
        unlink("/includes/uploads/gallery/".$row['imagename']);
        #lets delete the thumb.
        unlink("/includes/uploads/gallery/thumbs/".$row['thumbname']);
    }

            if($this->db->update('sales', $content))
                return TRUE;
            else
                return FALSE;
        } # End IF
} # End Update

I am having a really strange issue with my edit page:

If I have more then one sale:

If I have a single sale and upload a new image -> it saves the file name and thumb name but no image on server.

If I do not upload an image -> it overwrites the content on all other sales with the last edited sale but keeps the correct images.

If I upload an image it deletes both images and still overwrites the content of the last edited sale

I have tried this on a MAMP local server and a cPanel Server

print_r

Normal

Array ( [0] => Array ( [id] => 62 [name] => The Dream [location] => Otara [bedrooms] => 1 [bathrooms] => 1 [condition] => OK [description] =>
What a nice house

[price] => 30.00000 [imagename] => Shack.jpg [thumbname] => Shack_thumb.jpg ) )

Edit

Array ( [0] => Array ( [id] => 62 [name] => The Dream [location] => Otara [bedrooms] => 1 [bathrooms] => 5 [condition] => OK [description] =>
What a nice house

[price] => 30.00000 [imagename] => Mutton.jpg [thumbname] => Mutton_thumb.jpg ) ) 

View

<?php
//Setting form attributes
$formEditSale = array('id' => 'editSale', 'name' => 'editSale');
$formEditSaleName = array('id' => 'name', 'name' => 'name','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleLocation = array('id' => 'location', 'name' => 'location','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleBedrooms = array('id' => 'bedrooms','name' => 'bedrooms','class' => 'validate[required[custom[number]]]');
$formEditSaleBathrooms = array('id' => 'bathrooms','name' => 'bathrooms','class' => 'validate[required[custom[number]]]');
$formEditSaleCondition = array('id' => 'condition','name' => 'condition','class' => 'validate[required[custom[onlyLetterSp]]]');
$formEditSaleImage = array('id' => 'userfile', 'name'=> 'userfile');
$formEditSalePrice = array('id' => 'price','name' => 'price','class' => 'validate[required[custom[number]]]');
$formEditSaleDescription = array('id' => 'description','name' => 'description','class' => '');
$formEditSaleSubmit = array('id' => 'submit', 'name'=> 'submit', 'value' => 'Submit Edit');
?>

<div id ="formLayout" class="form">
    <?php
    if($success == TRUE) {
    echo '<section id = "validation">Sale Updated</section>';
    }

    echo '<section id = "validation">'.$message['imageError'].'</section>';
    ?>
<h4>You are editing sale: <?= $sale[0]['name']; ?></h4>
<?php echo form_open_multipart('admin/editsale/index/'.$sale[0]['id'].'/'.url_title($sale[0]['name'],'dash', TRUE),$formEditSale); ?>
<?php echo form_fieldset(); ?>

<?php echo form_label('Name:','name'); ?>
<?php echo form_input($formEditSaleName, $sale[0]['name']); ?>
<div id="errorName"><?php echo form_error('name'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Location:','location');?> 
<?php echo form_input($formEditSaleLocation, $sale[0]['location']);?>
<div id="errorLocation"><?php echo form_error('location'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Bedrooms: ','bedrooms');?>
<?php echo form_input($formEditSaleBedrooms, $sale[0]['bedrooms']);?>
<div id="errorBedrooms"><?php echo form_error('bedrooms'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Bathrooms: ','bathrooms');?>
<?php echo form_input($formEditSaleBathrooms, $sale[0]['bathrooms']);?>
<div id="errorBathrooms"><?php echo form_error('bathrooms'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Condition: ','condition');?>
<?php echo form_input($formEditSaleCondition, $sale[0]['condition']);?>
<div id="errorCondition"><?php echo form_error('condition'); ?></div>
<span class="small">Required Field - Text</span>

<?php echo form_label('Price: ','price');?>
<?php echo form_input($formEditSalePrice, $sale[0]['price']);?>
<div id="errorPrice"><?php echo form_error('price'); ?></div>
<span class="small">Required Field - Number</span>

<?php echo form_label('Image: ','userfile');?>
<?php echo form_upload($formEditSaleImage);?>
<div id="errorUserfile"><?php echo form_error('userfile'); ?></div>
<span class="small">1MB Max JPG/PNG</span>

<?php echo form_label('Description: ','description');?>
<div id="errorDescription"><?php echo form_error('description'); ?></div>
 <span class="small">Required Field - Text</span>
<?php echo form_textarea($formEditSaleDescription, $sale[0]['description']);?>
<script type="text/javascript">CKEDITOR.replace('description');</script>

<?php echo form_submit($formEditSaleSubmit);?>
<?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>
</div>

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Editsale extends CI_Controller {

        function __construct() {
        parent::__construct();

        }

        function index() {
           if(!$this->session->userdata('logged_in'))redirect('admin/home');

        $id = $this->uri->segment(4);
        # Set Main Page Data
            $data['title'] = 'Edit Sale:';
            $data['sales_pages'] = $this->sales_model->getSalesPages();
            $data['cms_pages'] = $this->navigation_model->getCMSPages();
            $data['sale']= $this->sales_model->getSalesContent($id);

             #Set The Validation Rules
                    $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('location', 'Location', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|numeric|required|xss_clean');
                    $this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|numeric|required|xss_clean');
                    $this->form_validation->set_rules('condition', 'Condition', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
                    $this->form_validation->set_rules('price', 'Price', 'trim|required|xss_clean');

                    if($this->form_validation->run()) #If Valid Run
                    {
                        $content = array(   
                            'name' => $this->input->post('name', TRUE), 
                            'location' => $this->input->post('location', TRUE), 
                            'bedrooms' => $this->input->post('bedrooms', TRUE), 
                            'bathrooms' => $this->input->post('bathrooms', TRUE), 
                            'condition' => $this->input->post('condition', TRUE), 
                            'description' => $this->input->post('description', TRUE), 
                            'price' => $this->input->post('price', TRUE)
                        );

                        if($_FILES['userfile']['error'] != 4) 
                        {  
                            //Set File Settings 
                              $config['upload_path'] = 'includes/uploads/gallery/'; 
                              $config['allowed_types'] = 'jpg|png'; 
                              $config['remove_spaces'] = TRUE;
                              $config['max_size'] = '1024'; 
                              $config['max_width'] = '1024'; 
                              $config['max_height'] = '768';

                            $this->load->library('upload', $config);
                            //Continue and resize the image
                            $data = array('upload_data' => $this->upload->data());
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;          #resize
                            $config['new_image'] = 'includes/uploads/gallery/thumbs/';
                            $config['create_thumb'] = 'TRUE';
                            $config['thumb_marker'] ='_thumb';
                            $config['maintain_ratio'] = 'FALSE';
                            $config['width'] = '175';
                            $config['height'] = '175';

                            $this->load->library('image_lib', $config);
                            $this->image_lib->resize(); 


                            if(!$this->upload->do_upload()) //Process the upload function and uploads image
                            {
                                $data['message'] = array('imageError' => $this->upload->display_errors()); // Capture any $config errors and output them
                            }
                            else
                            {
                                $file_info = $this->upload->data(); //Returns an data array 

                                // Append image data to content array in preparation for db update
                                $content['imagename'] = $file_info['file_name'];
                                $content['thumbname'] = $file_info['raw_name'].'_thumb'.$file_info['file_ext'];
                            }   
                        }   

                        // After everything, update DB

                        if($this->sales_model->updateSale($id, $content))
                        {
                            $data['success'] = TRUE;
                        }
                        else
                        {
                            $data['success'] = FALSE;
                        }

                 } # End Form Validation
            $data['cms_pages'] = $this->navigation_model->getCMSPages();
            $data['sales_pages'] = $this->sales_model->getSalesPages($id);
            $data['sale']= $this->sales_model->getSalesContent($id);
            $data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
            $this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it   
        } # End Index Function
    } # End Controller

Model

function updateSale($id, $content) { //Content id from being passed
    $this->db->where('id', $id);  // selecting the $id to update
    $update = $this->db->get('sales'); // What does $update = well it = get the db sales
    $row = $update->row_array(); // what does $row mean = well it gets the row as an array

    if($update->num_rows() > 0) {

        if(isset($content['imagename']) && isset($content['thumbname'])) {

        #lets delete the image
        unlink("/includes/uploads/gallery/".$row['imagename']);
        #lets delete the thumb.
        unlink("/includes/uploads/gallery/thumbs/".$row['thumbname']);
    }

            if($this->db->update('sales', $content))
                return TRUE;
            else
                return FALSE;
        } # End IF
} # End Update

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-11-13 12:11:51

$this->db->update('sales' 之前,您需要第二次调用 $this->db->where('id', $id) , $content)function updateSale()中调用,第一个在$this->db->get('sales')中使用> 查询,但您需要再次执行更新。

使用 $this->db->where('id', $id)->update('sales', $content)

You need a second call to $this->db->where('id', $id) before your $this->db->update('sales', $content) call in function updateSale(), the first one is used in the $this->db->get('sales') query, but you need to do it again for your update.

Use $this->db->where('id', $id)->update('sales', $content)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文