- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个广泛讨论的话题:如何在上传时将图像 URL 保存在数据库中?
我已阅读它们,但仍然无法理解为什么我的 Controller 无法工作。我关注了CodeIgniters documentation on uploading files并创建了将文件上传到所需目录的 Controller ,到目前为止一切顺利。我现在想在上传时将图像网址保存在数据库中。我修改了this question中的代码但我有多个无法解决的错误。
1.
Message: Illegal string offset 'file_name'
Filename: controllers/Upload.php
Line Number: 36
2.Message: Undefined property: Upload::$db
Filename: controllers/Upload.php
Line Number: 37
3.Message: Call to a member function insert() on null
Filename: controllers/Upload.php
Line Number: 37
这是我的 Controller (我有两个字段的测试表拼图 - 自动增量主键 id 和 image_url):
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$image['image_url'] = $file_array['file_name'];
$this->db->insert('puzz', $image);
$this->load->view('upload_success', $data);
}
}
}
?>
如果有人能指出我的代码中的错误,我将不胜感激!
最佳答案
在使用插入之前添加此内容
$this->load->database();
或
更改构造函数
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->database();
}
OR
更改 autoload.php
$autoload['libraries'] = array('database');
AND
将此代码块更改为
$data = $this->upload->data();
$image['image_url'] = $data['file_name'];
$this->db->insert('puzz', $image);
$this->load->view('upload_success', $data);
关于mysql - 上传时在数据库中存储图像 url 时出现问题 (CodeIgniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39892707/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!