CodeIgniter Forums
How do i insert a uploaded image's path to the database? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How do i insert a uploaded image's path to the database? (/thread-43526.html)



How do i insert a uploaded image's path to the database? - El Forum - 07-14-2011

[eluser]maxrosecollins[/eluser]
Hi,

I can upload an image to my site but i am having real trouble getting the path of the image into my database...?

this is my controller

Code:
<?php

class Sell extends CI_Controller
{    
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {
        $this->load->view('sell_view', array('error' => ' ' ));
    }

    function do_upload()
    {
        $this->load->model('sell_model');
        
        $config['upload_path'] = './uploads/';
        $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())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('sell_view', $error);
        }
        else
        {
            $this->sell_model->listProperty();
            
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('listed_view', $data);
        }
        
        
    }    
}


and my model...


Code:
<?php
class Sell_model extends CI_Model
{
    function getPropertyType()
    {
        $rows = $this->db->select('id, name')
            ->from('property_type')
            ->get()->result();    
        foreach ($rows as $row)
        {
            $property_type[$row->id] = $row->name;
        }
        return $property_type;
    }
    
    function listProperty()
    {
        $data = array($this->upload->data());
        
        $insert = array(
            'property_type_id'=>$this->input->post('property_type'),
               'bedrooms'=>$this->input->post('bedrooms'),
               'price'=>$this->input->post('price'),
               'images'=>$data['full_path'],
               'building_name'=>$this->input->post('building_name'),
               'road'=>$this->input->post('road'),
               'town'=>$this->input->post('town'),
               'county'=>$this->input->post('county'),
               'postcode'=>$this->input->post('postcode'),
               'tenure_type_id'=>$this->input->post('tenure'),
               'features'=>$this->input->post('features'),
               'description'=>$this->input->post('description'),
               'bathrooms'=>$this->input->post('bathrooms'),
               'seller_id'=>$this->input->post('seller_id')
        );
        $this->db->insert('sell_property', $insert);
    }
}



and my view


Code:
<?php echo $error;?>

    <?php echo form_open_multipart('sell/do_upload');?>

        <input type="file" name="userfile" size="20" />

        <br /><br />

        &lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;


and the other view.
which manages to show the path of the image i just can't understand why it wont let me insert it into the database!?!?!?

Code:
<div id="container">
    your property has been added
    
    <h3>Your file was successfully uploaded!</h3>

    <ul>
        &lt;?php foreach ($upload_data as $item => $value):?&gt;
            <li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
        &lt;?php endforeach; ?&gt;
    </ul>
    
    &lt;?php echo $upload_data['full_path']; ?&gt;

    <p>&lt;?php echo anchor('sell', 'Upload Another File!'); ?&gt;</p>
</div>

can anyone help me please?

it would be greatly appreciated

Thanks

Max


How do i insert a uploaded image's path to the database? - El Forum - 07-14-2011

[eluser]jmadsen[/eluser]
You don't have $data['full_path'] as part of your insert statement anywhere.

The file loading will take care of itself, but if you want its info stored in the db, you need to pass it there


How do i insert a uploaded image's path to the database? - El Forum - 07-16-2011

[eluser]maxrosecollins[/eluser]
isn't

Code:
'images'=>$data['full_path'],

all i need to put?!


How do i insert a uploaded image's path to the database? - El Forum - 07-16-2011

[eluser]jmadsen[/eluser]
Ah, I missed that, sorry.

Then use the standard debugging techniques and see where it is going wrong.

you can start with:

Code:
$this->db->last_query()


to see what your insert statement looks like, and if the value is there. Also, share any error messages.

"can't get it into the database" is rather vague for us to try to help you. could be your table schema, for example


How do i insert a uploaded image's path to the database? - El Forum - 07-16-2011

[eluser]fraserk[/eluser]
how about this..
Code:
$results = $this->upload->data();


Then

Code:
'images'=> $results['file_name']