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-18-2010

[eluser]cestaz18[/eluser]
hello everyone..

now i have a problem about file uploading and the same time in saving data...

the scenario is..

i have a one form to be filled up the data it asking including image file uploading..

then i have to save it but i got error message like this

Code:
<h1>A Database Error Occurred</h1>
        <p>Error Number: 1364</p><p>Field 'icon' doesn't have a default value</p><p>INSERT INTO `erp_sec_reports` (`module_name`, `slug`, `report_file_name`, `sql_select`, `sql_filter`, `sql_order`, `link`) VALUES ('aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'reports/view')


maybe i think the problem only is the code i'm using in file uploading bcoz the error i have is in the field icon where i'm passing the image file name i'm uploading..

here's my code use in uploading file...

could anyone can help me????

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:
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);
                $file_data = $this->upload->data();
                $file_name = $file_data['file_name'];
                
                
                
                    $fields = array(
                            "module_name",
                            "slug",
                            "icon"=>$file_data['file_name'],    
                            //'icon' => $file_name,                        
                            "report_file_name",
                            "sql_select",
                            "sql_filter",
                            "sql_order"
                            );

                    foreach ($fields as $field)
                    {
                        if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
                    }
                    
                    $item = $this->input->post("item");
                    $rows = $this->input->post("row0");
                            
                    $data['item'] = $this->Reports->save($dt,$item,$rows);    

                    $c .= 'Successfully saved item';
                }



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($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;
    }


my sample form image is given below for you to understand well my problem...

please help..

tnx in advance^^

Cess


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

[eluser]theprodigy[/eluser]
I don't see
Code:
$this->upload->do_upload()
anywhere in your code.


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

[eluser]cestaz18[/eluser]
[quote author="theprodigy" date="1263894805"]I don't see
Code:
$this->upload->do_upload()
anywhere in your code.[/quote]

ah i got it..
uhmm..for what that code is???

where i can put this code in my controller?


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

[eluser]cestaz18[/eluser]
i got the same problem in your code given mr.lab assistant


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

[eluser]theprodigy[/eluser]
Make sure you put it in your code, after
Code:
$this->load->library('upload', $config);
but before
Code:
$file_data = $this->upload->data();

$this->upload->data() won't have any info until after you call
Code:
$this->upload->do_upload()

Basically, you want
Code:
$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);

$this->upload->do_upload();    /***ADD IT HERE***/

$file_data = $this->upload->data();
$file_name = $file_data['file_name'];



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

[eluser]cestaz18[/eluser]
[quote author="theprodigy" date="1263895807"]Make sure you put it in your code, after
Code:
$this->load->library('upload', $config);
but before
Code:
$file_data = $this->upload->data();

$this->upload->data() won't have any info until after you call
Code:
$this->upload->do_upload()

Basically, you want
Code:
$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);

$this->upload->do_upload();    /***ADD IT HERE***/

$file_data = $this->upload->data();
$file_name = $file_data['file_name'];
[/quote]



oww..i got the same error..

Code:
<h1>A Database Error Occurred</h1>
        <p>Error Number: 1364</p><p>Field 'icon' doesn't have a default value</p><p>INSERT INTO `erp_sec_reports` (`module_name`, `slug`, `report_file_name`, `sql_select`, `sql_filter`, `sql_order`, `link`) VALUES ('adfasdf', 'asdfas', 'adfasd', 'adfasd', 'asdfsd', 'asdfas', 'reports/view')



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

[eluser]theprodigy[/eluser]
Quote:
Code:
foreach ($fields as $field)
{
    if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
}

$item = $this->input->post("item");
$rows = $this->input->post("row0");

$data['item'] = $this->Reports->save($dt,$item,$rows);

I think they may be your issue. When submitting a file upload. The file does NOT reside in the POST array. It gets put into the $_FILES array.
Therefore in your loop, when you are checking if it is set in the $_POST array, it skips over the "icon" element.


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

[eluser]maria clara[/eluser]
hi,

it seems that the problem is ryt there. the arrays there don't read the field for icon. i eliminate the foreach and the console shows a database error:


Code:
A Database Error Occurred
        <p>Error Number: 1054</p><p>Unknown column '0' in 'field list'</p><p>UPDATE `erp_sec_reports` SET `0` = 'module_name', `1` = 'slug', `icon` = '', `2` = 'report_file_name', `3` = 'sql_select', `4` = 'sql_filter', `5` = 'sql_order' WHERE `module_id` = '1'

i dont know where is that column '0'...
help please>....


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

[eluser]cestaz18[/eluser]
mr.lab assistant...
now i get your point..

i dont have anymore error

but doesnt save in database i cant see any records save in my database

but it dont have error message

here's my new code...

i insert it inside if condition including for each condition...

tell me why it doesnt save in database???

something wrong in my if condition??


Code:
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()){
                
                $file_data = $this->upload->data();
                $file_name = $file_data['file_name'];
            
                    
                     $fields = array(
                            "module_name",
                            "slug",
                            //"icon"=>$file_data['file_name'],    
                            "icon"=>$file_name,
                            //'icon' => $file_name,                        
                            "report_file_name",
                            "sql_select",
                            "sql_filter",
                            "sql_order"
                            );
                            
            

                    foreach ($fields as $field)
                    {
                        if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
                    }
                    
                    $item = $this->input->post("item");
                    $rows = $this->input->post("row0");
                            
                    $data['item'] = $this->Reports->save($dt,$item,$rows);    

                    $c .= 'Successfully saved item';
                    
                    
                }

                $data['response'] = $c;
                $json['json'] = $data;
                }
            }



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

[eluser]theprodigy[/eluser]
you might not be getting an error, because it may not be going into your
Code:
if ($this->upload->do_upload()){ ... }

statement. If it doesn't go into that, then it won't even try to save.

Don't get me wrong, that is the correct way of doing it, just make sure that the file is actually getting uploaded (check the folder, if no files are in there, check the write permissions of that folder. Make sure your website can write to that folder)

Also, the following code will not work.
Code:
$fields = array(
                    "module_name",
                    "slug",
                    //"icon"=>$file_data['file_name'],    
                    "icon"=>$file_name,
                    //'icon' => $file_name,                        
                    "report_file_name",
                    "sql_select",
                    "sql_filter",
                    "sql_order"
                );
                            
            

[b]foreach ($fields as $field)
{
    if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
}
[/b]                    
$item = $this->input->post("item");
$rows = $this->input->post("row0");

$data['item'] = $this->Reports->save($dt,$item,$rows);

Your foreach loop will loop over that array just fine, but your check to see if it isset in the $_POST array will fail when it comes to the
Quote:"icon"=>$file_name,
part, because "icon" does not exist in the $_POST array. Why don't you just manually set the fields in the $fields array, like you are for the "icon" field.

Code:
$fields = array(
                    "module_name"=>$this->input->post('module_name'),
                    "slug"=>$this->input->post('slug'),
                    "icon"=>$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')
                );

and just get rid of the foreach loop and pass in the $fields array instead.
Or loop through the fields array like you are, but unset any elements that equal false, to get rid of any fields that weren't passed in.