【问题标题】:File upload in codeigniter with other input field使用其他输入字段在 codeigniter 中上传文件
【发布时间】:2016-10-03 12:41:59
【问题描述】:

我研究了文件上传的codeigniter文档并实现了只有一个文件字段的文件上传,但现在我还有其他输入字段,所以我使用了下面的代码但出现DATABASE错误“列'userfile'不能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);
        } } }
?>

【问题讨论】:

  • 错误号:1048 列 'userfile' 不能为空 INSERT INTO validate_tablenameemaildatenotesuserfile)值('fcv' , 'harsh.sharma1943@gmail.com', '10/12/2016', 'vxcv', NULL) 文件名:C:/xampp/htdocs/codeigniter/system/database/DB_driver.php 行号:691
  • 你不能有 $this->input->post('userfile');它由 $_FILES['userfile'] 访问
  • @AbuHurairaLakdawala 但我们在核心 php 中使用 $_FILES,你能写代码吗。
  • 一些明智的代码缩进可以大大提高代码的可读性,从而更容易调试
  • 对不起@RiggsFolly 让我编辑它。

标签: php codeigniter


【解决方案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

【讨论】:

  • 图像名称保存到数据库中,但不保存到上传文件夹中
  • 在此处设置您的文件夹路径。 $config['upload_path'] = './uploads/';
  • 这应该在加载库之前进行
  • "在此处设置您的文件夹路径。$config['upload_path'] = './uploads/';" - 他们已经这样做了$config['upload_path'] = './uploads/';。 - 对于为什么没有上传,我有自己的看法。
  • 也检查这个网址:

猜你喜欢
  • 2017-12-01
  • 1970-01-01
  • 2015-06-04
  • 2020-07-25
  • 2016-07-16
  • 1970-01-01
  • 2014-05-19
  • 2013-10-18
相关资源
最近更新 更多