Welcome Guest, Not a member yet? Register   Sign In
form upload question
#1

[eluser]karloff[/eluser]
I've edited this post as i solved the last problem, i can't pass the image path into the database. I'm sure it fairly simple but can't get the syntax correct

i get this error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: image

Filename: controllers/admin.php

Line Number: 96
A Database Error Occurred

Error Number: 1136

Column count doesn't match value count at row 1

INSERT INTO entries (title, image, pub_date, body) VALUES ('test work',NULL, NOW(),'test comments go here', 'http://codeigniter.com/')

not sure why it won't pass the image path to db, i followed the user guide (well i thought so, obviously i've gone wrong some where

here is part of my controller
Code:
...............
    function work_list()
    {
        $this->db->orderby('pub_date', "desc");
        $data['query'] = $this->db->get('work');    
        $this->load->view('admin/work_view', $data);
        $this->db->orderby('pub_date', "desc");


    }    

    function work_add()
    {
    
    $this->load->view('admin/work_add_view');
    
    }
    
    
    function work_insert()
    {


        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '380';
        $config['max_height']  = '130';
        $field_name = "image";

        $title = $_POST['title'];
        $body = $_POST['body'];
        $image = $_POST['image'];
        $url = $_POST['url'];        


//        $this->upload->work_insert($image);
        $this->load->library('upload', $config);
        $this->db->query("INSERT INTO entries (title, image, pub_date, body) VALUES (".$this->db->escape($title).",".$this->db->escape($image).", NOW(),".$this->db->escape($body).", ".$this->db->escape($url).")");
    
        redirect('admin/work_list');
    

        
        
        
    }..................
and here's my view
Code:
<?php $this->load->view('header'); ?>

<p>&lt;?=anchor('admin/main', 'Back to Main'); ?&gt;</p>

<h2>Work Details</h2>
    &lt;?=form_open('admin/work_insert')?&gt;
        <fieldset>
            <legend>Entry Details</legend>
                <li><label>Headline</label></li>
                <li>&lt;?=form_input('title',''); ?&gt;</li>
                <li><label>Full Story</label></li>
                <li>&lt;? $data = array(
                      'name'    => 'body',
                    'style'   => 'width:100%',
                    );
                    echo form_textarea($data);
                 ?&gt;</li>
                 <li>&lt;input type="file" name="image" size="20" /&gt;</li>
                <li>&lt;?=form_input('url',''); ?&gt;</li>
                  <li>&lt;? $data = array(
                      'name'    => 'submit',
                    'class'   => 'submit',
                    'type'    => 'submit',
                    'value'    => 'Save changes',
                    );
                    echo form_submit($data);
                 ?&gt;</li>


        </fieldset>
    &lt;?=form_close(); ?&gt;

&lt;?php $this->load->view('footer'); ?&gt;

any help is much appreciated
#2

[eluser]karloff[/eluser]
i've updated my post so it may be clearer if anyone can shine some light
#3

[eluser]Colin Williams[/eluser]
Wow.

Quote:Column count doesn’t match value count at row 1

INSERT INTO entries (title, image, pub_date, body) VALUES (’test work’,NULL, NOW(),’test comments go here’, ‘http://codeigniter.com/’)

Your column count is 4. Your values count is 5. Easy 'nuff.

Also, this mishap can be avoiding by using the Active Record's insert() method rather than writing it out like that. And please don't say you didn't know you could do that. If nothing else, read through the Database Class part of the User Guide. Twice Smile
#4

[eluser]karloff[/eluser]
i appreciate the advice, still not sorted totally, but I'll go and have read and make sure i cover all the classes before asking a stupid question

cheers for the heads up
#5

[eluser]Colin Williams[/eluser]
Meh.. not a stupid question at all. Hope you get it working.
#6

[eluser]karloff[/eluser]
right, back to this silly question, i can upload and the image and isert the image, however i get the following errors


Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: userfile

Filename: controllers/admin.php

Line Number: 88
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\ci\system\libraries\Exceptions.php:164)

Filename: helpers/url_helper.php

Line Number: 485

here is my controller
Code:
function work_insert()
    {

        $title = $_POST['title'];
        $body = $_POST['body'];
        $userfile = $_POST['userfile'];
        $url = $_POST['url'];        
//        $date = NOW();


        $this->db->set('title', $title);
        $this->db->set('body', $body);
        $this->db->set('userfile', $userfile);
        $this->db->set('url', $url);
        $this->db->insert('work');

        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $data['userfile'] = array('name' => 'userfile');
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            $this->load->view('admin/work_add_view', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            redirect('admin/work_list');
        }
        
            
    }

i also tried the $this->db->insert(); function but no joy
#7

[eluser]libnac[/eluser]
In order to access file name you must try $_FILES['image']['name'] and in your form ensure to put on the property enctype='multipart/form-data'.
Also you must try to call do_upload like this way:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image')) ... <-- image is the field name in your form &lt;input type='file' name='image'&gt;
#8

[eluser]Colin Williams[/eluser]
Quote:and in your form ensure to put on the property enctype=’multipart/form-data’.

I bet this is the problem. Get to know your PHP errors. Undefined index means an array index was called upon but it did not exist. So, if CI tries to hit $_FILES['image'] and $_FILES['image'] does not exist, BAM!! Error on your ass.
#9

[eluser]karloff[/eluser]
manage to get it working by passing

'userfile' => $data['upload_data']['file_name'], to the $data array.

didn't need to set

if ( ! $this->upload->do_upload(’image’)) as do_upload retrieves the value of userfile by default if no parameters are passed in

also realised there was no need to post the userfile to a variable

much to learn for me,

cheers guys
#10

[eluser]charlie spider[/eluser]
another way is to just always name your form's file input field 'userfile' since that's what php expects it to be




Theme © iAndrew 2016 - Forum software by © MyBB