删除图像的强制选项无法在mysql数据库中插入帖子

时间:2022-10-06 13:45:04

Hi i am having a page where user can insert the blogs which contains blog name,image and description.Getting the problem while inserting blogs in database.If i am removing the mandatory option for blog image unable to insert the post into database.It is displaying blog has successfully inserted but there is no blog inserted in my database.

嗨,我有一个页面,用户可以插入包含博客名称,图像和描述的博客。在数据库中插入博客时获取问题。如果我删除博客图像的强制选项无法将帖子插入数据库。它是显示博客已成功插入,但我的数据库中没有插入博客。

Controller:

控制器:

function addblogs()
{
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
    $this->form_validation->set_rules('blog_title','Blog Title');
    $this->form_validation->set_rules('description','Blog Description');        
    if($this->form_validation->run()== FALSE)   
    {       
    $data['mainpage']='blogs';
    $data['mode']='add';
    $this->load->view('templates/template',$data);
    }
    else
    {
        $this -> blogs_model -> insertblogs();
        $this->flash->success('<h2>blogs Added Successfully!</h2>');
        redirect('blogs');
    }
}

Model:

模型:

function insertblogs()
{               
    $data=array(        
    'blog_title'=>$this->input->post('blog_title'),
    'description'=>$this->input->post('description'),       
    );
     if ( $_FILES AND $_FILES['image_path']['name'])
            {
                $file_name = $this->do_upload2();
                if(is_array($file_name)){
                    $error['imageerror'] = $file_name['error'];
                }
                else
                $data['image_path']=$file_name;
            }
            if(!isset($data['image_path']) && !isset($error['imageerror']))
            $error['imageerror'] ="Please Upload an image";
            if(isset($error))return $error;     
            $this->db->insert('blogs',$data);

} 

function do_upload2() 
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->image_path,
        'max_size' => 20000,
        'maintain_ratio'=>FALSE,
        'width' => 90,
        'height' => 75
    );
    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('image_path'))
    {
        return $error = array('error' => $this->upload->display_errors());
    }
    else
    {
        $image_data = $this->upload->data();
        $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' =>$this->image_path . '/thumbs',
        'maintain_ratio' => FALSE,
        'width' => 90,
        'height' => 75
        );
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        $filename =time().preg_replace('/[^A-Za-z0-9\s.\s-]/', '_', $image_data['file_name']);
        rename($image_data['full_path'],$image_data['file_path'].$filename);
        rename($image_data['file_path'].'thumbs/'.$image_data['file_name'],$image_data['file_path'].'thumbs/'.$filename);
        return $filename;
    }
}

View:

视图:

<script type="text/javascript">
            $(document).ready(function() {  
            $('#submit').click(function(){
                    if((jQuery.trim($('#image_path').val())==''))
                    {
                        alert("Please select image");
                        $('#image_path').focus();
                        return false;
                    }
                });
            }); 
        </script>           
    <div class="full_w">
    <div class="h_title">
        <div class="lefttitle fl">
            Add Blogs
        </div>
        <div class="rightbutton fr">
            <a  class="button cancel" href="<?php echo site_url()?>/blogs">Cancel</a>
        </div>
    </div>
    <?php
        $form_attributes  = array('name'=>'adds', 'id'=>'adds', 'enctype' => "multipart/form-data");
        echo form_open('blogs/addblogs',$form_attributes);
    ?>

    <div class="element">
        <label for="blogtitle"><font color ="black">Blog Title</font></label>
        <input class="text err" type="text" name="blog_title" id="blog_title" value="<?php echo set_value('blog_title');?>"/>               
    </div>          
    <div class="element">
        <label for="description"><font color ="black">Blog Description</font></label>
        <textarea name="description" class="ckeditor" rows="4" cols="173"></textarea>
    </div> <br/>

    <div class="element">
        <label><font color ="black">Select Image:</font></label>
        <input class="err" type="file" name="image_path" id="image_path"/>          
    </div><br/>

    <div align="center">
            <input type="submit" id="submit" value="Submit" />
    </div> 
    <div class="clear"></div>
    <?php echo form_close();?>
    </div>
    </div>
    <div class="clear"></div>

I am using codeigniter php

我正在使用codeigniter php

1 个解决方案

#1


1  

From insert blog section commented three lines now its working fine.

从插入博客部分评论三行现在它的工作正常。

if ( $_FILES AND $_FILES['image_path']['name'])
        {
            $file_name = $this->do_upload2();
            if(is_array($file_name)){
                $error['imageerror'] = $file_name['error'];
            }
            else
            $data['image_path']=$file_name;
        }
        //if(!isset($data['image_path']) && !isset($error['imageerror']))
        //$error['imageerror'] ="Please Upload an image";
        //if(isset($error))return $error;     

#1


1  

From insert blog section commented three lines now its working fine.

从插入博客部分评论三行现在它的工作正常。

if ( $_FILES AND $_FILES['image_path']['name'])
        {
            $file_name = $this->do_upload2();
            if(is_array($file_name)){
                $error['imageerror'] = $file_name['error'];
            }
            else
            $data['image_path']=$file_name;
        }
        //if(!isset($data['image_path']) && !isset($error['imageerror']))
        //$error['imageerror'] ="Please Upload an image";
        //if(isset($error))return $error;