0

I'm trying to upload an image with a car make, year, lease and payment data along with the image upload. My image path is also inserted to the DB but i still cant figure how to combine both. Im getting an error Array to string conversion and its not inputting my data and image upload.

cars table:

car_make | car_model | car_year | car_lease | car_payment | imgpath
acura    | G37 Sedan |   2009   |  48       |   350       |  assets/images/acura_1.jpg

controller:

public function car_validation_do_upload() {

        $this->load->library('form_validation');


        $this->form_validation->set_rules('car_make', 'Car Make', 'required');
        $this->form_validation->set_rules('car_model', 'Car Model', 'required');
        $this->form_validation->set_rules('car_year', 'Car Year', 'required');
        $this->form_validation->set_rules('car_lease', 'Car Lease', 'required');
        $this->form_validation->set_rules('car_payment', 'Car Payment', 'required');

        if($this->form_validation->run() == TRUE) {

            $config['upload_path'] = './assets/images/';
            $config['allowed_types'] = 'gif|jpeg|jpg|png';
            $this->load->library('upload', $config);




            $newRow = array("car_make" => $this->input->post('car_make'),
                        "car_model" => $this->input->post('car_model'),
                        "car_year" => $this->input->post('car_year'),
                        "car_lease" => $this->input->post('car_lease'),
                        "car_payment" => $this->input->post('car_payment'));

            $data = array('upload' => $this->upload->data());

            $result = array_merge($newRow, $data);

        $this->load->model("model_users");

        $this->model_users->insert1($result);   
        $this->session->set_flashdata('message', 'car inserted into database!');
        redirect('main/insertCar');


    }


    else {
    $this->load->view('insert_car');
    }


    }


Model:
public function insert1($data) {

        $this->db->insert("cars", $data);
        }

View
<?php 

    //echo form_open('main/car_validation_insert');
    echo form_open_multipart('main/car_validation_do_upload');  
        echo validation_errors();

        if($this->session->flashdata('message')){
  echo $this->session->flashdata('message');
  }
    ?>

    <table width="504" style="margin-left:15px;">
<tr>
<td colspan="2"><p class="table-title">Insert Car</p></td>
<td></td>

</tr>

<tr>
<td>Car Make: </td>
<td><input type="text" name="car_make" class="textbox" tabindex="1"/></td>
<td>&nbsp;&nbsp;&nbsp;</td>


</tr>
<tr>
  <td>Car Model:</td>
  <td><input type="text" name="car_model" class="textbox" tabindex="2"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Year:</td>
  <td><input type="text" name="car_year" class="textbox" tabindex="3"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Lease:</td>
  <td><input type="text" name="car_lease" class="textbox" tabindex="4"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Payment:</td>
  <td><input type="text" name="car_payment" class="textbox" tabindex="5"/></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>Car Image:</td>
  <td><input type="file" name="userfile" size="20" /></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>

</table>


<input type="submit" name="submit" value="submit" id="button" class="button" style="margin-left:140px;"  />

<?php

echo form_close();
?>
1
  • Im getting error when inserting values to table Commented Mar 7, 2013 at 21:22

1 Answer 1

1

Change this:

$data = array('upload' => $this->upload->data());

$result = array_merge($newRow, $data);

To this:

  if ($this->upload->do_upload()){
    $image_data = $this->upload->data();
    //this will create a new key with the name upload in your newRow
    //so theres no need to use the function array_merge
    $newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
}
else{
    //do something here
    echo 'error uploading image'
}

The $image_data variable will have this information about your uploaded image

 Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

You can find all the information about the file upload class on the user guide of codeingiter: http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Sign up to request clarification or add additional context in comments.

13 Comments

Hi, i tried your code it saying Undefined variable: array. I already read the user guide, my form is more complicated because I am trying to upload text and image to the same table. what else can I try? thanks.
for this line: $newRow['upload'] = $array['full_path']; Am I suppose to change the 'full_path' to something else?
sorry, instead of $array it should be $image_data
I think Im getting closer. The error is Unknown column 'userfile' in 'field list' INSERT INTO cars (car_make, car_model, car_year, car_lease, car_payment, imgpath, userfile) VALUES ('bmw', '525i', '2004', '24', '350', 1, 'C:/wamp/www/autoline/assets/images/bmw5.jpg')
On the answer, it was edited, but here you have again $newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.