我正在使用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
答案 0 :(得分:0)
您无法在控制器中将数据作为数组传递 更改
$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;
}
}
}
答案 1 :(得分:0)
无需转到另一个函数image_validation()。您可以在以下相同的else子句中插入数据。
public function check_img()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if($this->form_validation->run() == false)
{
$this->index();
}
else
{
//$data['name'] = $this->input->post('name');
//$this->image_validation($data['name']);
$name = $this->input->post('name');
if( $_FILES['userfile']['name']!='' && $_FILES['userfile']['size'] > 0 )
{
//NOTE :- To give uploaded image your Input field name, pass the $name variable to $config['file_name'] name.
$config['file_name'] = $name;
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
//load library
$this->load->library('upload');
$this->upload->initialize($config);
if( $this->upload->do_upload('userfile') )
{
$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;
}
else
{
$error = $this->upload->display_errors();
$this->form_validation->set_message('image_validation', $error);
return false;
}
}
else
{
//show error to select Image.
}
}
}