gpt4 book ai didi

php - 删除图像的强制选项,无法将帖子插入 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 10:38:38 28 4
gpt4 key购买 nike

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

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

型号:

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

查看:

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

我正在使用 codeigniter php

最佳答案

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

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;

关于php - 删除图像的强制选项,无法将帖子插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45956771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com