Welcome Guest, Not a member yet? Register   Sign In
DB Insert Problem (Active Record)
#1

[eluser]Sayian[/eluser]
Hey all,

I'm receiving an error message when attempting to run the following insert query using active record.

The error shown is:

A Database Error Occurred
You must use the "set" method to update an entry.

The page operation is as follows:
Code:
function do_upload()
    {
        $config['upload_path'] = user_dir_path($this->sess_user_id);
        $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
        $config['max_size']    = '2056';
        $config['max_width']  = '1024';
        $config['max_height']  = '1024';
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload())
        {
           // DEBUG
           echo($this->upload->display_errors());
        }
        else
        {
          $data = $this->upload->data();

          // insert photo into mysql
          $this->_insert_photo($data);

          // create thumbnail of image
          $this->_resize($data['full_path']);

          // load view file
          $this->load->view('account/photos');
        }
    }

The line that calls the DB Insert and throws the error is $this->_insert_photo($data).

I'm sure you'll want to see that function, so here it is.

Code:
function _insert_photo($array)
    {

      // setup array of data to pass to model insert function
      $sql = array(

        'user_id'  =>  $this->sess_user_id,
        'main'     =>  '1',
        'filename' =>  $array['file_name'],
        'upload_time' => time(),
        'title'   =>   'test'

        );

      // run insert operation and grab the boolean
      $op = $this->account_photos_model->insert_photo($sql);

      // if operation failed, show the user an error message.
      if($op == FALSE)
      {
        show_error('Photo upload failed due to database error.');
      }
    }

... and finally the code that is in the model file for the actual DB insert.

Code:
function insert_photo($sql)
    {
      $this->db->insert('photos'.$sql);

      if($this->db->affected_rows() == TRUE)
      {
        return TRUE;
      }
      
      // uh oh ...
      return FALSE;
}

I've gone over this several times and don't understand why it's trying to update the record set instead of creating a new one (obviously, there is nothing to update since the primary key is set to auto increment).

Thanks for the help.
#2

[eluser]rogierb[/eluser]
Code:
$this->db->insert('photos'.$sql);

There is a dot instead of a comma. Check if that is in your code or just a c/p error




Theme © iAndrew 2016 - Forum software by © MyBB