Php 如何使用codeigniter存储图像并从文件夹中检索?
我是CodeIgniter编程新手。我想从文件夹中存储和检索图像。但当我运行代码时,我发现错误如下: 第一个错误:Php 如何使用codeigniter存储图像并从文件夹中检索?,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我是CodeIgniter编程新手。我想从文件夹中存储和检索图像。但当我运行代码时,我发现错误如下: 第一个错误: Upload failed! A PHP Error was encountered Severity: Notice Message: Undefined variable: in Filename: controllers/main.php Line Number: 104 第二个错误: A Database Error Occurred You must use
Upload failed!
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: in
Filename: controllers/main.php
Line Number: 104
第二个错误:
A Database Error Occurred
You must use the "set" method to update an entry.
Filename: application/models/main_model.php
Line Number: 80
我正在使用以下代码:
控制:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
$this->load->helper(array('form', 'url'));
}
public function product()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('productname','Product Code','trim|required');
$this->form_validation->set_rules('productcode','Product Code','trim|required');
$this->form_validation->set_rules('productprice','Product Price','trim|required');
$this->form_validation->set_rules('quantity','Quantity','trim|required');
$this->form_validation->set_rules('uploadimage','Upload Image','trim|required');
if($this->form_validation->run()==FALSE)
{
$this->index();
}else
{
if ($this->input->post('upload'))
{
$in=array();
$in['productname'] = $this->input->post('productnamename');
$in['productcode'] = $this->input->post('productcode');
$in['productprice']=$this->input->post('productprice');
$in['quantity']=$this->input->post('quantity');
$in['uploadimage']=$_FILES['image']['name'];
}
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}else
{
$this->main_model->save_gallery($in);
header('location:product');
}
$data['images']=$this->main_model->get_images();
$this->load->view('query_view',$data);
}
}
1) 您的表单enctype
必须是multipart
2) 您正在检查post
而不是$\u文件
它应该是if($this->input->post('upload'))
到if($\u FILES['upload']['name'])
由于这个原因,它不会出现在if块中,因此调用时$in
是未定义的
$this->main_model->save_gallery($in);
当$in
未定义时,因此您会看到erro必须使用“set”方法更新条目。
在模型中
希望它有意义1)您的表单enctype
必须是multipart
2) 您正在检查post
而不是$\u文件
它应该是if($this->input->post('upload'))
到if($\u FILES['upload']['name'])
由于这个原因,它不会出现在if块中,因此调用时$in
是未定义的
$this->main_model->save_gallery($in);
当$in
未定义时,因此您会看到erro必须使用“set”方法更新条目。
在模型中
希望它有意义首先,请阅读位于的文档。一字不差,没有花哨的东西,没有数据库,没有额外的条件。让它工作起来,并在很小的部分中增量添加新功能。当你遇到问题时,环顾四周寻找答案
现在来看问题
您没有得到$this->input->post('upload')
的值,这只是许多问题中的一个
你的表格应该是开放的
下面的错误通常表示您正在向insert
函数传递一个空变量
A Database Error Occurred
You must use the "set" method to update an entry.
Filename: application/models/main_model.php
Line Number: 80
为什么要传递一个空变量?您正在调用总是返回void的do\u upload
方法,因此下面的代码中永远不满足TRUE
条件
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}
else
{
$this->main_model->save_gallery($in);
header('location:product');
}
验证您正在表单中传递名为upload
的输入,并且上传的条件应该更像这样
if(isset($_FILES['upload']['name']))
{
$in=array();
$in['productname'] = $this->input->post('productnamename');
$in['productcode'] = $this->input->post('productcode');
$in['productprice']=$this->input->post('productprice');
$in['quantity']=$this->input->post('quantity');
$in['uploadimage']=$_FILES['image']['name'];
// moved to inside the post('upload') conditional
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}
else
{
$this->main_model->save_gallery($in);
header('location:product');
}
}
首先,请阅读位于的文档。一字不差,没有花哨的东西,没有数据库,没有额外的条件。让它工作起来,并在很小的部分中增量添加新功能。当你遇到问题时,环顾四周寻找答案
现在来看问题
您没有得到$this->input->post('upload')
的值,这只是许多问题中的一个
你的表格应该是开放的
下面的错误通常表示您正在向insert
函数传递一个空变量
A Database Error Occurred
You must use the "set" method to update an entry.
Filename: application/models/main_model.php
Line Number: 80
为什么要传递一个空变量?您正在调用总是返回void的do\u upload
方法,因此下面的代码中永远不满足TRUE
条件
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}
else
{
$this->main_model->save_gallery($in);
header('location:product');
}
验证您正在表单中传递名为upload
的输入,并且上传的条件应该更像这样
if(isset($_FILES['upload']['name']))
{
$in=array();
$in['productname'] = $this->input->post('productnamename');
$in['productcode'] = $this->input->post('productcode');
$in['productprice']=$this->input->post('productprice');
$in['quantity']=$this->input->post('quantity');
$in['uploadimage']=$_FILES['image']['name'];
// moved to inside the post('upload') conditional
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}
else
{
$this->main_model->save_gallery($in);
header('location:product');
}
}
如何使用set方法dianuj?你能告诉我吗?我是codeigniter编程新手。你不必使用set
CI将自动使用你只需通过$in
值,目前,您发送的$in
为空或未定义,因此一旦有值就会发生错误。如何使用set方法dianuj?请告诉我。我是codeigniter编程新手。您不必使用set
CI将自动使用。您只需将$in
与值一起传递即可,当前,您发送的$in
为空或未定义,因此一旦有值就会发生错误,它将正常工作