Welcome Guest, Not a member yet? Register   Sign In
Save image path to database / edit function not editing entry but inserting new one
#1

[eluser]ede196620[/eluser]
Hello everyone I am gonna try to kill two birds in one stone here by asking two questions. My first question is how to grab uploaded files name and store it in to a database i have the upload file function set up but cant find a way to save the path to a table.

And my second question is about updating an entry in a database i have the function set up and was making some modifications to it and it stopped updating and instead it started creating new ones and i daunt know what i did for this to happen and cant revers the changes. This is still a work in progress.

Any help is greatly appreciated here are the functions that are used in the process

Code:
// controller
public function edit($id = NULL) {
        //get product or set a new one
        if ($id) {
            //get product if there is id
            $this->data['product'] = $this->product->get($id);
            
            //if the user with id not found show error mesage
            count($this->data['product']) || $this->data['errors'][] = 'Product Not Found';
        } else {
            
            //get new product if there is no id
            $this->data['product'] = $this->product->get_new_product();
        }
        //Uplode image
        //Configure upload
        $config['uploade_path'] = 'application/uploads';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1500';
        $config['max_width']  = '390';
        $config['max_height']  = '269';
        //load the upload library
        $this->load->library('upload', $config);
        
        $this->upload->initialize($config);
        
       // $this->upload->set_allowed_types('*');
        
        $this->data['upload_data'] = '';
        //if not successful, set the error message
        if (!$this->upload->do_upload('img_path')) {
            //fail message
            $data = array('msg' => $this->upload->display_errors());
        } else { //else, set the success message
            //sucess message
            $data = array('msg' => "Upload success!");

            $this->data['upload_data'] = $this->upload->data();
        }//end uplode task
      

      // $id == NULL || $this->data['product'] = $this->product->get($id);

        $rules = $this->product->rules;
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {
            $data = $this->product->array_from_post(array('product_name', 'body', 'sell_or_trade', 'img_path', 'price', 'category_id', 'id', 'order'));
            $this->product->save($data, $id);
            redirect('site_user/product_c');
        }
        $this->data['category'] = $this->category->get();
        $this->data['subview'] = 'site_user/edit';
        //load the view
        $this->load->view('site_user/_layout_main', $this->data);
    }

Code:
public function get_new_product() {

        $product = new stdClass();
        $product->product_name = '';
        $product->body = '';
        $product->sell_or_trade = '';
        $product->price = '';
        $product->img_path = '';
        $product->category_id = '';
        $product->id = '';

        return $product;
    }

Code:
public function array_from_post($fields) {
        $data = array();
        foreach ($fields as $field) {
            $data[$field] = $this->input->post($field);
        }
        return $data;
    }

Code:
public function save($data, $id = NULL) {
        //set timestamps
        if ($this->_timestamps == TRUE) {
            $now = date('Y-m-d H:i:s');
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }
        //Insert
        if ($id === NULL) {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL || $data[$this->_primary_key];
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
            //Update  
        } else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }
        return $id;
    }
#2

[eluser]CroNiX[/eluser]
What's this?
Code:
$id = $filter($id);

As far as getting the file path...it's containted in $this->upload->data(), which returns an array of attributes. See towards the bottom of http://ellislab.com/codeigniter/user-gui...ading.html

You also misspelled update:
Code:
$this->db->updata($this->_table_name);
#3

[eluser]ede196620[/eluser]
Hi CroNiX tnx for your replay

I fixed the spelling error but same thing is happening

and as for the

Code:
$id = $filter($id);

this filters the id which is the primary key
#4

[eluser]InsiteFX[/eluser]
Maybe this:
Code:
$this->db->updata($this->_table_name);

// should be:
$this->db->update($this->_table_name);
#5

[eluser]ede196620[/eluser]
i fixed the misspelled world but it is not that
#6

[eluser]InsiteFX[/eluser]
Do a echo var_dump($id); in your methods to see if your getting the id, if it never does the update then it's always inserting because $id is null.
#7

[eluser]ede196620[/eluser]
well in the controller edit functions i did var_dump($id); and it returned an id that belongs to the selected item

these are set in MY_Model and my product model
Code:
class MY_Model extends CI_Model {

    protected $_table_name = '';
    protected $_primary_key = 'product_id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    public $rules = array();
    protected $_timestamps = FALSE;

Code:
lass Product extends MY_Model {

    protected $_table_name = 'products';
    protected $_primary_key = 'product_id';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'id';
    protected $_timestamps = FALSE;
    public $rules = array(
#8

[eluser]Tim Brownlaw[/eluser]
You want to Check what $id where it's being used...
See the added code...
Code:
public function save($data, $id = NULL) {
        //set timestamps

// >>> ADDED CODE <<<
// Adding a var_dump here to see what my value of $id really is when I land in this method...
echo "id in method save is ...";
var_dump($id);
// >>> End of ADDED CODE <<<

        if ($this->_timestamps == TRUE) {
            $now = date('Y-m-d H:i:s');
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }
        //Insert
        if ($id === NULL) {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL || $data[$this->_primary_key];
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
            //Update  
        } else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }
        return $id;
    }

You may have to add in an exit(); after the var_dump to stop it running to catch the output.

And work back from there...

Also it wouldn't hurt to do a var_dump of $data to ensure that is also correct...
This is a case of "test" the different bits to make sure they are working, being passed the expected values... Don't guess!
#9

[eluser]ede196620[/eluser]
sorry for the long wait i did a var_dump($id) and var_dump($data)
the $id is not returning anything
and the data dose return this array
Code:
array (size=8)
  'product_name' => string 'test product2222' (length=16)
  'body' => string 'test product description goes here' (length=34)
  'sell_or_trade' => string 'sell' (length=4)
  'img_path' => boolean false
  'price' => string '2222' (length=4)
  'category_id' => string '5' (length=1)
  'id' => string '1' (length=1)
  'order' => boolean false
what can i do to fix this ?
#10

[eluser]InsiteFX[/eluser]
Where is $filter?

You may need to add this:
Code:
$id = $this->filter($id);

Also you did not show your input form, the problem could be there.

If $id is null then it is not being passed from your form to your controller.




Theme © iAndrew 2016 - Forum software by © MyBB