html - 带输入名称字段的图像上传
问题描述
我正在使用 Codeigniter 并尝试在数据库中上传带有用户名的图像名称。在我的视图文件中有两个字段,一个是输入字段,第二个是文件类型输入字段。但是当我尝试将第一个输入字段值从一个函数传递到另一个函数时出现问题,它显示为空白。这是我的代码...
控制器
class img_upload extends CI_Controller
{
public function index()
{
$data['title'] = 'Image Upload & Display';
$this->load->view('img_upload_view',$data);
}
public function check_img()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('userfile', 'Userfile', 'callback_image_validation');
if($this->form_validation->run() == false)
{
$this->index();
}
else
{
$data['name'] = $this->input->post('name');
$this->image_validation($data['name']);
}
}
public function image_validation($data)
{
$name = $data['name'];
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
$this->form_validation->set_message('image_validation', $error);
return false;
}
else
{
$data = $this->upload->data();
$img = $data['file_name'];
$data = array(
'name' => $name,
'img_name' => $img
);
$this->load->model('img_model');
$this->img_model->Insert_Data($data);
$this->session->set_flashdata('success', "Data Inserted Successfully!");
return true;
}
}
}
模型
class img_model extends CI_Model
{
public function Insert_Data($data)
{
$this->db->insert('image', $data);
}
}
看法
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-8">
<br>
<div class="card">
<div class="card-body">
<h5 class="card-title"><h2><?php echo $title; ?></h2></h5>
<p class="card-text">
<?php if($this->session->flashdata('success') == '')
{
echo $this->session->flashdata('success');
}
?>
<?php echo form_open_multipart('img_upload/check_img'); ?>
<div class="form-group">
<label for="exampleInputName">Name</label>
<input type="text" class="form-control" placeholder="Enter username" name="name">
<span><?php echo form_error('name');?></span>
</div>
<div class="form-group">
<label for="exampleInputUpload">Upload</label>
<input type="file" class="form-control" name="userfile">
<span><?php echo form_error('userfile');?></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br>
<div class="img-reponsive">
<!-- <img src="<?php #echo 'upload/'.$images->img_name; ?>" height="150" width="150"> -->
</div>
</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
错误
A Database Error Occurred
Error Number: 1048
Column 'name' cannot be null
INSERT INTO `image` (`name`, `img_name`) VALUES (NULL, 'c4.jpg')
Filename: C:/xampp/htdocs/image_upload/system/database/DB_driver.php
Line Number: 691
解决方案
您不能在控制器更改中将数据作为数组传递
$data['name'] = $this->input->post('name');
$this->image_validation($data['name']);
至
$name = $this->input->post('name');
$this->image_validation($name);
尝试这个
public function check_img(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('userfile', 'Userfile', 'callback_image_validation');
if($this->form_validation->run() == false)
{
$this->index();
}
else
{
$data['name'] = $this->input->post('name');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpeg|jpg|png|gif';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else{
$upload_data = $this->upload->data();
$data['img_name'] = $upload_data['file_name'];
$this->load->model('img_model');
$this->img_model->Insert_Data($data);
$this->session->set_flashdata('success', "Data Inserted Successfully!");
return true;
}
}
}
推荐阅读
- javascript - 事件处理程序未委托给父元素
- flutter - 尝试在 BottomAppBar 中嵌入 TextField 或 TextFormField
- php - PHP调用数组中的索引位置
- openlayers - 在 openlayers 的共享视图中使用同一层
- postgresql - 使用 Shell 脚本将 JSON 文件插入到 postgres
- php - 分类页面 - 更好的图像检索解决方案?n * scandir() OR n* mysql 查询或完全连接?
- opencv - 实时关键点检测算法
- javascript - 从数组中的项目中提取重复字符串
- reactjs - React.js 错误:服务工作者导航预加载请求在“preloadResponse”解决之前被取消
- swift - SwiftUI 上的 iOS 14 小部件与情节提要上的项目