CodeIgniter Forums
failed to upload image file and save data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: failed to upload image file and save data (/thread-26620.html)

Pages: 1 2 3 4


failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
here's my complete code...

VIEW
Code:
<div class="infield">
                                    &lt;?php echo form_open_multipart('at_reports/post');?&gt;
                                    &lt;input type="file" name="userfile" class="tb" title="&lt;?=lang('icon');?&gt;"/&gt;
                                    &lt;input type="submit" value="upload" /&gt;
                                    &lt;!--&lt;/form&gt;--&gt;
                                    &lt;!--&lt;input type="text" name="icon" class="tb" title="&lt;?=lang ('icon');?&gt;" /&gt;--&gt;
                                </div>

CONTROLLER
Code:
[code]

&lt;?php
    class At_reports extends Controller {
        function At_reports() {
            parent::Controller();
            $method = $this->uri->rsegment(2);
            $user = $this->session->userdata('user');
            if (($method!='login'&&$method!='auth'&&$method!='logout')&&!$user) redirect('at_reports');

            $this->load->helper('form');
            $this->load->model('At_reports_db', 'Reports');
            $this->load->model('Dataset_db');

        
            
            $lang_select = $this->session->userdata('lang_select');
            $this->config->set_item('language', $lang_select);
            $this->lang->load('at_reports');
        }

        function index() {
            $data['showtoolbar'] = true;
            $data['ptitle'] = "Admin Tool - Reports";
            $data['navs'] = $this->Dataset_db->buildNav(0);
            $data['report_navs'] = $this->Dataset_db->buildReportNav(0);
            $data['admin_navs'] = $this->Dataset_db->buildAdminToolNav(0);
            $data['current_main_nav'] = 'admin';

            //tab management
            $tabs = $this->session->userdata('tabs');
            if (!$tabs) $tabs = array();
            $tabs['at_reports'] = $this->Dataset_db->getAdminTool('at_reports');
            $this->session->set_userdata('tabs',$tabs);
            $data['current_tab'] = $tabs['at_reports']['link'];

            $data['operator'] = $this->Dataset_db->getSearchTypes('OPERATOR');
            $data['content'] = $this->load->view('home',$data,true);
            $this->load->vars($data);
            $this->load->view('default_view');
        }

        function listview() {
            $data = $this->Reports->getList();
            $this->load->view('template/table',$data);
        }

        function detaillistview() {
            //$dt['module_id'] = $this->input->post('item');
            $data = $this->Reports->getParameters();
            $this->load->view('template/table',$data);
        }

        function js() {
            $this->load->view('js/script.js');
        }

       [code]



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
CONTINUATION OF CONTROLLER

Code:
function post() {
            //$this->output->enable_profiler(true);
            $c = "";
            $data['action'] = $this->input->post('action');
            

            
            if ($data['action']=='view') {
                $dt['module_id'] = $this->input->post('item');
                $report['report_id'] = $this->input->post('item');
                $details = $this->Reports->getDetails($dt);

                if ($details) {
                    $data['db'] = array('item'=>$dt['module_id'],
                                        'module_name'=>$details['module_name'],
                                        'slug'=>$details['slug'],
                                        'icon'=> $details['icon'],                                        
                                        'report_file_name'=>$details['report_file_name'],
                                        'sql_select'=>$details['sql_select'],
                                        'sql_filter'=>$details['sql_filter'],
                                        'sql_order'=>$details['sql_order']
                                        );
                    $data['count'] = $this->Reports->countDetailList($report);
                    $c .= $this->Reports->getParameters($report);
                }
                $data['response'] = $c;
                $json['json'] = $data;
            }
           [b] else if ($data['action']=='save') {
                $item = $this->input->post("item");
                $dt['module_name'] = $this->input->post('module_name');
    
                $report_exist = $this->Reports->getDetailsByName($dt);

                if ($report_exist && $item != $report_exist['module_id'])
                {
                    $c .= 'Report already exists.';
                    $data['action'] = 'exist';
                }
                else
                {
                
                $config['upload_path'] = './reports/module/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '100';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                
                 /*if ($this->upload->do_upload())
                {*/
                    $this->upload->do_upload();
                    $file_data = $this->upload->data();
                    //$file_name = $file_data['file_name'];
            
                    
                    
                    $fields = array(
                    "module_name"=>$this->input->post('module_name'),
                    "slug"=>$this->input->post('slug'),    
                   "icon"=>$this->input->post('file_name'),          
                    "report_file_name"=>$this->input->post('report_file_name'),
                    "sql_select"=>$this->input->post('sql_select'),
                    "sql_filter"=>$this->input->post('sql_filter'),
                    "sql_order"=>$this->input->post('sql_order')
                    );        
                    
            

                
                    
                    $item = $this->input->post("item");
                    $rows = $this->input->post("row0");
                            
                   // $data['item'] = $this->Reports->save($form_data,$fields,$item,$rows);    
                    $data['item'] = $this->Reports->save($fields,$item,$rows);    
                    
                    //Insert it here
                    echo $this->db->last_query();
                    
                    $c .= 'Successfully saved item';
                    
                    
              /*  }*/[/b]
                }
                $data['response'] = $c;
                $json['json'] = $data;
                
            }



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
CONTINUATION OF CONTROLLER

Code:
else if ($data['action']=='delete') {
                $dt['module_id'] = $this->input->post("item");
                $data['deleted_row'] = $this->Reports->delete($dt);

                //audit trail
                $log['tran_id'] = $dt['module_id'];
                $log['tran_no'] = $dt['module_id'];
                $log['module_name'] = $this->input->post("module");
                $log['event_name'] = lang('deleted');
                $this->Dataset_db->saveLog($log);

                $c .= lang('deleted_item').$data['deleted_row'];

                $data['response'] = $c;
                $json['json'] = $data;
            }
            $this->load->view('template/ajax',$json);
        }
    }
?&gt;



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
MODEL

Code:
&lt;?

class At_reports_db extends Model {

    function At_reports_db()
    {
        parent::Model();
    }

    function getList()
    {
        $sortname = $this->input->post('sortname');
        $sortorder = $this->input->post('sortorder');
        $page = $this->input->post('page');
        $rp = $this->input->post('rp');

        $query = $this->input->post('query');
        $qtype = $this->input->post('qtype');

        $stype = $this->input->post('s_type');
        $otype = $this->input->post('o_type');
        $skey = $this->input->post('s_key');

        if (!$sortname) $sortname = 'module_id';
        if (!$sortorder) $sortorder = 'DESC';

        if (!$page) $page = 1;
        if (!$rp) $rp = 25;

        $start = (($page-1) * $rp);

        $this->db->start_cache();

        if ($stype)
        {
            foreach ($stype as $key => $value)
            {
                if ($skey[$key])
                {
                    if ($stype[$key]=='so_date')  $match = convertDate($skey[$key]);
                    else $match = $skey[$key];
                    
                    switch ($otype[$key])
                    {
                        case "=":
                            $this->db->where($stype[$key],$match);
                            break;
                        case "!=":
                            $this->db->where($stype[$key]." !=",$match);
                            break;
                        case "like":
                            $this->db->like($stype[$key],$match);
                            break;
                        case ">":
                            $this->db->where($stype[$key]." >",$match);
                            break;
                        case "<":
                            $this->db->where($stype[$key]." <",$match);
                            break;
                    }
                }
            }
            
        }


        $this->db->from('sec_reports');
        $this->db->select('module_id as pkey, module_name');
        
        $num = $this->db->count_all_results();

        $this->db->order_by($sortname,$sortorder);

        $this->db->limit($rp, $start);
        $query = $this->db->get("sec_reports");

        $this->db->flush_cache();

        $data['db'] = $query;
        $data['page'] = $page;
        $data['num'] = $num;
        return $data;

    }

    function countDetailList($data)
    {
        $this->db->where($data);
        $this->db->from('sec_reports_parameters');
        $count['detFlex1'] = $this->db->count_all_results();

        return $count;
    }

    function getDetails($cond) {
        $data['module_id'] = $cond['module_id'];
        $this->db->select("*");
        $this->db->from("sec_reports");
        $this->db->where($data);
        
        return $this->db->get('sec_reports')->row_array();
    }

function getDetailsByName($cond) {
        $data['module_name'] = $cond['module_name'];
        $this->db->select("*");
        $this->db->from("sec_reports");
        $this->db->where($data);

        return $this->db->get('sec_reports')->row_array();
    }
function getParameters() {
        $item = $this->input->post('item');
        $sortname = $this->input->post('sortname');
        $sortorder = $this->input->post('sortorder');

        if (!$sortname) $sortname = 'parameter_id';
        if (!$sortorder) $sortorder = 'ASC';

        $page = 1;

        $this->db->start_cache();

        $this->db->from('sec_reports_parameters');
        //$this->db->where('sec_reports_parameters.parameter_id',$item);
        $num = $this->db->count_all_results();

        $this->db->select("parameter_id as pkey,parameter_id, parameter_label, parameter_name, parameter_type");
        $this->db->order_by($sortname,$sortorder);
        $query = $this->db->get("sec_reports_parameters");

        $this->db->flush_cache();

        $data['db'] = $query;
        $data['page'] = $page;
        $data['num'] = $num;
        return $data;
        //return $num;
    }



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
CONTINUATION OF MODEL

Code:
function save_detail($id,$rows) //Query for saving the company details
    {
        $this->db->where('report_id', $id);
        $this->db->delete('sec_reports_parameters');
        
        foreach ($rows as $row)
        {
            $rdata = explode('|', $row);
            
            $data['report_id'] = $id;
            $data['parameter_label'] = $rdata[1];        
            $data['parameter_name'] = $rdata[2];    
            $data['parameter_type'] = $rdata[3];        
            
            $this->db->insert('sec_reports_parameters', $data);
        }
    }

  /*  function save($file_name,$data,$item,$details) {
        if ($item == '') {
            $data['link'] = "reports/view";
            $this->db->trans_start();
            $this->db->insert('sec_reports', $data);
            $this->db->insert('sec_reports', $file_name);
            $item = $this->db->insert_id();
            $this->save_detail($item,$details);            
            $this->db->trans_complete();
        
        }*/
        
        function save($data,$item,$details) {
        if ($item == '') {
            $data['link'] = "reports/view";
            $this->db->trans_start();
            $this->db->insert('sec_reports', $data);
            $item = $this->db->insert_id();
            $this->save_detail($item,$details);            
            $this->db->trans_complete();
            }
        else {
            $this->db->trans_start();
            $this->db->where('module_id', $item);
            $this->db->update('sec_reports', $data);
            $this->save_detail($item,$details);                
            $this->db->trans_complete();
        }
        return $item;
    }

    function delete($data)
    {
        $deleted_row = $data['module_id'];

        $this->db->where('module_id', $data['module_id']);
        $this->db->delete('sec_reports', $data);

        return $deleted_row;
    }
}

?&gt;



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
we are talking problem in function POST in controller and in model..we are asking also about function SAVE..that's the code we using in saving..

hope you can look on the problem..

thanks a lot ^^


failed to upload image file and save data - El Forum - 01-19-2010

[eluser]theprodigy[/eluser]
Please update your controller. In your post() method, I still see:

Quote:$fields = array(
"module_name"=>$this->input->post('module_name'),
"slug"=>$this->input->post('slug'),
"icon"=>$this->input->post('file_name'),
"report_file_name"=>$this->input->post('report_file_name'),
"sql_select"=>$this->input->post('sql_select'),
"sql_filter"=>$this->input->post('sql_filter'),
"sql_order"=>$this->input->post('sql_order')
);

In your view, I also only see:

Quote:<div class="infield">
&lt;?php echo form_open_multipart('at_reports/post');?&gt;
&lt;input type="file" name="userfile" class="tb" title="&lt;?=lang('icon');?&gt;"/&gt;
&lt;input type="submit" value="upload" /&gt;
&lt;!--&lt;/form&gt;--&gt;
&lt;!--&lt;input type="text" name="icon" class="tb" title="&lt;?=lang ('icon');?&gt;" /&gt;--&gt;
</div>

The controller part needs to be updated with the fact that you cannot retrieve the file name from the $this->input->post() method. It simply isn't there. You need to get the file name from the $file_data array.

In your view, it doesn't look like the form elements are all there, and you aren't closing your form tag (the close form tag is commented out, as is the icon input field). Am I not seeing the full form? Do you have other parts of the form being added on later?


failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
full VIEW code

Code:
<div id="Header" class="panel">
    &lt;form class="form"&gt;
    <div class="fields">
    &lt;input type="hidden" name="action" value="save" /&gt;
    &lt;input type="hidden" name="method" value="/post" /&gt;
    &lt;input type="hidden" name="module" value="at_reports" /&gt;
    &lt;input type="text" name="item" style="display:none;" /&gt;

    <div class="content">

       <h2>&lt;?=lang('module_label');?&gt;</h2>

            <div class="ptoggle"><a class="uppane" title="Hide/Show Pane"></a></div>
            <div class="dpane headpane">
                <div class="fpane" style="display: block;">

                    <div class="fcolumn1 tView">
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('module_name','module_name');?&gt;</div>
                            <div class="field fw">
                                    <div class="infield">
                                        <span class="parameters"></span>
                                        <span class="module_name"></span>
                                    </div>
                            </div>
                        </div>
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('report_file_name','report_file_name');?&gt;</div>
                            <div class="field fw">
                                    <div class="infield">
                                        <span class="report_file_name"></span>
                                    </div>
                            </div>
                        </div>


<div class="frow">
                                <div class="flabel">&lt;?=lang('icon','icon');?&gt;</div>
                                <div class="field fw">
                                        <div class="infield">
                                        <span class="icon"></span>
                                        </div>
                                </div>
                        </div>
                        <div class="frow">
                                <div class="flabel">&lt;?=lang('sql_select','sql_select');?&gt;</div>
                                <div class="field fw">
                                        <div class="infield">
                                        <span class="sql_select"></span>
                                        </div>
                                </div>
                        </div>
                        <div class="frow">
                                <div class="flabel">&lt;?=lang('sql_filter','sql_filter');?&gt;</div>
                                <div class="field fw">
                                        <div class="infield">
                                        <span class="sql_filter"></span>
                                        </div>
                                </div>
                        </div>



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
continuation VIEW code

Code:
<div class="frow">
                            <div class="flabel">&lt;?=lang('sql_order','sql_order');?&gt;</div>
                            <div class="field fw">
                                <div class="infield">
                                        <span class="sql_order"></span>
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;
                    </div>


                    <div class="fcolumn1 tEdit">
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('module_name','module_name');?&gt; </div>
                            <div class="field fw">
                                <div class="infield">
                                    &lt;input type="text" name="module_name" class="tb" title="&lt;?=lang('module_name');?&gt;" /&gt;
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('slug','slug');?&gt; </div>
                            <div class="field fw">
                                <div class="infield">
                                    &lt;input type="text" name="slug" class="tb" title="&lt;?=lang('slug');?&gt;" /&gt;
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;                        
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('report_file_name','report_file_name');?&gt; </div>
                            <div class="field fw">
                                <div class="infield">
                                    &lt;input type="text" name="report_file_name" class="tb" title="&lt;?=lang('report_file_name');?&gt;" /&gt;
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;
                        <div class="frow">
                            <div class="flabel">&lt;?=lang('icon','icon');?&gt; </div>
                            <div class="field fw">
                                <div class="infield">
                                    &lt;?php echo form_open_multipart('at_reports/post');?&gt;
                                    &lt;input type="file" name="userfile" class="tb" title="&lt;?=lang('icon');?&gt;"/&gt;
                                    &lt;input type="submit" value="upload" /&gt;
                                    &lt;!--&lt;/form&gt;--&gt;
                                    &lt;!--&lt;input type="text" name="icon" class="tb" title="&lt;?=lang ('icon');?&gt;" /&gt;--&gt;
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;
                        <div class="frow">
                                <div class="flabel">&lt;?=lang('sql_select','sql_select');?&gt;</div>
                                <div class="field fw">
                                    <div class="infield">
                                        &lt;textarea name="sql_select" class="report-ta" title="&lt;?=lang('sql_select');?&gt;" &gt;&lt;/textarea>
                                    </div>
                                </div>
                        </div>&lt;!--frow--&gt;
                        <div class="frow">
                                <div class="flabel">&lt;?=lang('sql_filter','sql_filter');?&gt;</div>
                                <div class="field fw">
                                    <div class="infield">
                                        &lt;textarea name="sql_filter" class="report-ta" title="&lt;?=lang('sql_filter');?&gt;" &gt;&lt;/textarea>
                                    </div>
                                </div>&lt;!--frow--&gt;
                        </div>


<div class="frow">
                            <div class="flabel">&lt;?=lang('sql_order','sql_order');?&gt;</div>
                            <div class="field fw">
                                <div class="infield">
                                    &lt;input type="text" name="sql_order" class="tb" title="&lt;?=lang('sql_order');?&gt;"/&gt;
                                </div>
                             </div>
                        </div>&lt;!--frow--&gt;
                    </div>&lt;!--fcolumn1--&gt;
                <div style="clear:both"></div>
                </div>
                </div>&lt;!--fpane--&gt;
                <div>
                    <table id="detFlex1" class="detFlex"></table>
                </div>
            &lt;!--dpane--&gt;
    </div>&lt;!--content--&gt;
    </div>

<div class="response content"></div>
    &lt;/form&gt;

</div>



failed to upload image file and save data - El Forum - 01-19-2010

[eluser]cestaz18[/eluser]
continuation of VIEW code

Code:
<div id="List" class="panel" style="display:block">
&lt;form id="sform"&gt;
    <div class="topfields">
       <div class="topsearch">
                <div>
                <select name="s_type[]" title="Search Type" autocomplete="off">
                </select>
                <select name="o_type[]" title="Operator Type" autocomplete="off">
                    &lt;?
                    foreach($operator as $item)
                    {
                        if ($item->data_value != '<' && $item->data_value != '>') {
                            echo "<option value='".$item->data_value."'>".$item->data_display."</option>";
                        }
                    }
                    ?&gt;
                </select>
                &lt;input type="text" name="s_key[]" class="tb" title="Search Key" autocomplete="off" /&gt;
                </div>
            </div>
            <button type="button" class="button" title="Add Form"><span class="bt1"><span class="bt2"><b class="search_add"></b></span></span></button>

          <div class="topsearchadd"></div>
    </div>
&lt;/form&gt;

    <div class="content">
            <div class="dpane">
                   <table class="listFlex"></table>
            </div>
    </div>
</div>

<div id="Editor">

<div class="dfEditor" id="df_editor" title="&lt;?=lang('edit_item');?&gt;" style="display:none;">
            &lt;form&gt;

            &lt;input type="text" name="index" value="" style="display:none" /&gt;
            &lt;input type="text" name="id" value=""  style="display:none" /&gt;
            <div class="dfEditor_inner">
                    <div class="df_row">
                            <div class="flabel">&lt;?=lang('parameter_name', 'parameter_name');?&gt;</div>
                            <div class="df_field">
                                    &lt;input type="text" name="parameter_name" class="tb rtb" title="&lt;?=lang('parameter_name');?&gt;" value="" autocomplete="off" /&gt;
                                    &lt;input type="text" name="parameter_id" style="display:none" /&gt;
                            </div>
                    </div>

                    <div class="df_row">
                            <div class="flabel">&lt;?=lang('parameter_label', 'parameter_label');?&gt;</div>
                            <div class="df_field">
                                    &lt;input type="text" name="parameter_label" class="tb" value="" autocomplete="off" /&gt;
                            </div>
                    </div>

                    <div class="df_row">
                            <div class="flabel">&lt;?=lang('parameter_type', 'parameter_type');?&gt;</div>
                            <div class="df_field">
                                    <select name="o_type[]" title="Operator Type" autocomplete="off">
                                        <option value="string">String</option>
                                        <option value="number">Number</option>
                                        <option value="date">Date</option>
                                    </select>
                            </div>
                    </div>
                    <div class="df_row">
                        &lt;input type="submit" value="Save" /&gt;
                    </div>
            </div>
            &lt;/form&gt;
    </div>
</div>