我已经研究了文件上传的codeigniter文档,并且只用一个文件字段实现了文件上传,但是现在我还有其他输入字段,所以我使用了下面的代码但是有DATABASE错误“列'用户文件'不能是null“,请告诉我我错在哪里
视图
<html>
<body>
<head>
<title>VALIDATION</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
<script>
$(document).ready(function(){
$("#sub").click(function(){
alert("Please check all required fields are filled.");
});
});
</script>
</head>
<form method="POST" action="<?php echo site_url('Dob_control/insert');?>" enctype="multipart/form-data">
<p>NAME:</p>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50">
<p>EMAIL:</p>
<?php echo form_error('email'); ?>
<input type="email" name="email" value="<?php echo set_value('email'); ?>" size="50">
<p>DATE:</p>
<?php echo form_error('date'); ?>
<input type="date" id="datepicker" name="date" value="<?php echo set_value('date'); ?>" size="50">
<p>NOTES:</p>
<?php echo form_error('notes'); ?>
<textarea name="notes" rows="4" cols="39"><?php echo set_value('notes'); ?></textarea>
<p>IMAGE:</p>
<input type="file" name="userfile" size="20" />
<br>
<input type="submit" id="sub" name="submit" value="submit">
</form>
</body>
</html>
模型
<?php
class Dob_model extends CI_Model {
function __construct(){
parent::__construct();
$this->load->database();
}
function insert_record($student){
$this->db->insert('validate_table', $student); // insert data into `validate_table table`
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
}
?>
控制器
<?php
class Dob_control extends CI_controller {
function __construct(){
parent::__construct();
$this->load->model('Dob_model');
}
function index(){
$this->load->helper(array('form','url'));
$this->load->view('simple_form');
}
public function insert(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('date','Date','required');
$this->form_validation->set_rules('notes','Notes','required', array( 'required' => 'Please complete this field'));
if ($this->form_validation->run() == FALSE)
{
$this->load->view('simple_form');
} else {
if ($this->input->post('submit') == true){
$student = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'date' => $this->input->post('date'),
'notes' => $this->input->post('notes'),
'userfile'=> $this->input->post('userfile'));
$result = $this->Dob_model->insert_record($student);
if($result==true){
echo "inserted";
}else
echo "Not Inserted";
} } }
public function do_upload(){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10244;
$config['max_height'] = 7685;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->Dob_control->display_errors());
$this->load->view('simple_form', $error);
} else {
$data = array('upload_data' => $this->Dob_control->data());
$this->load->view('upload_success', $data);
} } }
?>
答案 0 :(得分:1)
首先加载库。
$this->load->library('upload', $config);
然后上传您的文件
if ( $this->upload->do_upload('userfile'))
{
$data = array('upload_data' => $this->upload->data());
// $data will contain your file information
}
请参阅https://codeigniter.com/user_guide/libraries/file_uploading.html