filename返回空codeigniter

时间:2016-11-08 17:16:06

标签: php codeigniter

我有表格在php上传图片,当我保存路径和名称时,名称返回表数据库中的空字段这里是我的代码,我的数据库中的值总是空字段,请帮帮我

注册控制器

<?php
/**
* 
*/
class Registration extends CI_Controller {

    function __construct() {
        parent::__construct();
        //$this->load->library('upload');
        $this->load->helper('form');
    }

    function index() {
        $this->load->view('/registration/daftar');

    }

    function add_user() {
        $this->load->model('/daftar/Daftar_Model');
        $image_path = APPPATH. 'user_images/';
        $config['upload_path']   = $image_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '500';
        $config['max_height']    = '450';
        $config['max_width']     = '450';
        //$config['encrypt_name']  = TRUE;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $path = $this->upload->data();
        $image_name1 = $path['file_name'];
        $image_path1 = $path['file_path'];
        //$this->upload->do_upload('foto');
        //$photo_data = $this->upload->data();

        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'email' => $this->input->post('email'),
            'no_telepon' => $this->input->post('no_telepon'),
            'alamat' => $this->input->post('alamat'),
            'image_path' => $image_path1,
            'image_name' => $image_name1
            );

        $this->db->select('username');
        $this->db->where('username', $data['username']);
        $result = $this->db->get('user');

        if($result->num_rows() > 0) {
            echo "Udah ada";
            $this->output->set_header('refresh:2; url='.site_url("/registration"));

        } else {
            if(! $this->upload->do_upload('foto')) {
                var_dump($this->upload->display_errors());
                //$this->load->view('/registration/daftar', $error);
            } else {
                //$data1 = array('upload_data' => $this->upload->data());
                $this->Daftar_Model->insert($data);
                echo "Registrasi Berhasil";
                $this->output->set_header('refresh:2; url='.site_url("/login"));
                //$this->load->view('/registration/daftar', $data1);
            }
        }
    }
}

这是我的表格

<html>
<head>
    <title>Daftar</title>
    <body>
    <?php
    ?>
        <h1><center>Daftar</center></h1>
        <?php echo form_open_multipart('registration/registration/add_user'); ?>
        <center>
            Username <br>
            <input type="text" name="username"> <br>
            Password <br>
            <input type="password" name="password"> <br>
            Email <br>
            <input type="text" name="email"> <br>
            No Telepon <br>
            <input type="text" name="no_telepon"> <br>
            Alamat <br>
            <textarea name="alamat" rows="6" cols="23"></textarea> <br>
            </form>
            Foto
            <input type="file" name="foto"><br><br><br>
            <input type="submit" name="submit" value="Daftar">
        </center>
        </form>
    </body>
</head>
</html>

1 个答案:

答案 0 :(得分:0)

您是否检查了您的功能add_user()

我建议您在添加到数据库之前检查上传状态。如果您的上传代码错误,他们会显示错误,然后您将知道在哪里修复它。它应该是:

if(!$this->upload->do_upload('foto'))
{
    // Print the upload error
    var_dump($this->upload->display_errors());
}else{
    $return = $this->upload->data();
    $image_name = $return['file_name'];
    // your code add to database
}

希望得到这个帮助。