上传图像文件并存储MYSQL Codeigniter

时间:2014-09-15 02:51:13

标签: php mysql codeigniter

我正在尝试通过表单将图片上传到我的网站,我的表单出现问题,实际上不知道该去哪里:

<?php echo form_open_multipart('login/save');?>   

        <table>

            <tr>
                <td>Title</td>
                <td><input type="text" name="title"></input></td>
            </tr>    
            <tr>
                <td><input type="file" name="userfile" size="20" /> </td>
            </tr> 
            <tr>
                <td><input type="submit" name="submit" value="submit"></input></td>
            </tr>       
        </table>
    <?php
        echo form_close();
    ?>

我的控制器:

<?php
class Login extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url'); 
    }
    public function index(){
        $this->load->model('mlogin');
        $this->load->view('login/a', array('error' => ' ' ));
    }
    public function save(){

            $this->load->model("Mlogin");        
            if($this->input->post("submit")){

            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            if ( function_exists( 'date_default_timezone_set' ) ) date_default_timezone_set('Asia/Ho_Chi_Minh');
            $date = date("His_dmY_");
            $config['file_name']  = $date;
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('login/a', $error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

            }       
                }
            //redirect("login/a");


            $this->Mlogin->add();   
                echo "Success!";
                }


}        
?>

我的模特:

<?php
class Mlogin extends CI_Model{
    public function __construct(){
        parent::__construct();
        $this->load->database();


    }

    public function add(){
        $title = $this->input->post("title");
        $data = array(
            "title"=>$title,
            "image"=>$file_name
            );
        $this->db->insert("abc",$data);    
            }//end of simpan        
 }      
?>

我应该做些不同的事吗?不确定出了什么问题。 的错误:

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:file_name

     

文件名:models / mlogin.php

     

行号:23

     

发生数据库错误

     

错误号码:1054

     

冠军'头衔'无名字段列表

     

INSERT INTO abctitleimage)VALUES('342342',NULL)

     

文件名:C:\ wamp \ www \ system \ database \ DB_driver.php

     

行号:330

0 个答案:

没有答案
相关问题