Welcome Guest, Not a member yet? Register   Sign In
Help uploading file path to database
#1

[eluser]ICS4U1[/eluser]
Hello, I am trying to upload an image path to a database and cannot figure it out.

Code:
//data model @@@@@@@@@@@@@


function getAll() {
  
  $q = $this->db->get('data');
  
  if($q->num_rows() > 0) {
   foreach ($q->result() as $row) {
    $data[] = $row;
   }
   return $data;
  }  
  
}
function add_record($data)
{
  $this->db->insert('data', $data);
  return;
}

//IMAGE @@@@@@@@@@@@@@@@@@@@@

var $gallery_path;
var $gallery_path_url;

function data_model() {
  
  $this->gallery_path = realpath(APPPATH . '../upload');
  $this->gallery_path_url = 'http://localhost/OscarProject/upload/';
}

function do_upload() {
  
  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png',
   'upload_path' => $this->gallery_path,
   'max_size' => 2000
  );
  
  $this->load->library('upload', $config);
  $this->upload->do_upload();
  $image_data = $this->upload->data();
  
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->gallery_path . '/thumbs',
   'maintain_ratio' => true,
   'width' => 150,
   'height' => 100
  );
  
  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
  
  
}

function get_images() {
  
  $files = scandir($this->gallery_path);
  $files = array_diff($files, array('.', '..', 'thumbs'));
  
  $temp = array();
  
  foreach ($files as $file) {
   $temp []= array (
    'url' => $this->gallery_path_url . $file,
    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
   );
   $images = $temp[0];
  }
  
  return $images;
}



CONTROLLER
Code:
// CONTROLLER

function create()
{
$this->load->model('data_model');
  $data = array(
   'item' => $this->input->post('item'),
   'price' => $this->input->post('price')/*,
   'image' => $this->input->post('image')*/
  );
  if ($this->input->post('temp')) {
   $this->data_model->do_upload();
  }
  
  $data['images'] = $this->data_model->get_images();
  $this->load->model('data_model');
  $this->data_model->add_record($data);
  
  $this->crud();

}

VIEW

Code:
// VIEW

</tr>
    <td> &lt;?php echo $row ->item;?&gt;</td>
    <td> &lt;?php echo $row ->price;?&gt;</td>
    <td>&lt;?php echo anchor("site/delete/$row->id", "Delete"); ?&gt;</td>
    <td>&lt;?php echo anchor("site/update/$row->id", "Update"); ?&gt;</td>
    <td><a><img src="&lt;?php echo base_url().$row-&gt;images;?&gt;"></a></td>
    
    
    
    </tr>
    &lt;?php endforeach ;?&gt;
    
    
    </table>
    &lt;?php endif; ?&gt;

    
   </div>
      <h2>Create</h2>
   &lt;?php echo form_open('site/create');?&gt;
  
   <p>
    <label for="title">Item:</label>
    &lt;input type="text" name="item" id="item" /&gt;
   </p>
  
   <p>
    <label for="content">Price:</label>
    &lt;input type="text" name="price" id="price" /&gt;
   </p>
  
   <p>
    <label for="content">Image:</label>
#2

[eluser]Cristian Gilè[/eluser]
What is exactly your issue?
#3

[eluser]ICS4U1[/eluser]
Im trying to get the upload to store the image path inside a database. It can upload the text but nulls the image path and nothing is stored
#4

[eluser]Cristian Gilè[/eluser]
Try to debug your app using $this->upload->display_errors(). More info here: http://ellislab.com/codeigniter/user-gui...ading.html
#5

[eluser]ICS4U1[/eluser]
PHP Error
Message: Undefined variable: images

Filename: models/data_model.php

Line Number: 142

DATABASE Error
Error Number: 1048

Column 'images' cannot be null

INSERT INTO `data` (`item`, `price`, `images`) VALUES ('ghsdf', '4', NULL)

Filename: C:\wamp\www\OscarProject\system\database\DB_driver.php

Line Number: 330




Theme © iAndrew 2016 - Forum software by © MyBB