0

I am trying to get the max value from a database table via this CodeIgniter query:

$this->db->select_max('product_id');
$this->db->from('products');
$query = $this->db->get();
return $query->result();

But when I add this array of objects to to my next query, I get an error:

$this->db->insert('images', $product_id);

It show this error:

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO images (product_id) VALUES (Array)

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

Line Number: 330

When I did var_dump(), it showed:

array(1) {
    ["product_id"]=> array(1) {
        [0]=> object(stdClass)#21 (1) {
            ["product_id"]=> string(2) "26"
        }
    }
}
4
  • please post your controller and model code for better understand your problem Commented May 5, 2015 at 7:48
  • The error is pretty clear - you are inserting an Array instead of an actual value, so why don't you check $product_id before inserting? You are not showing us the code that gets $product_id from the result, and there's the issue. Commented May 5, 2015 at 7:48
  • php It's telling you that $product_id is an array, use print_r($product_id) to see which values it contains. Commented May 5, 2015 at 7:49
  • If you use $this->db->insert('images', $this->db->select_max('product_id')->get('products')->row_array()); this should provide a flat array. Although such practice seems vulnerable to race conditions. Commented 22 hours ago

3 Answers 3

1

You are applying an array within input here's I have updated your code

Model Code

$this->db->select_max('product_id');
$this->db->from('products');
$query=$this->db->get();
return $query->result_array();

Controller Code

$product_id = $this->Product_area_model->get_product_id();
$data['product_id'] = $product_id[0]['product_id'];
$result = $this->your_model->foo($data);

Model Code

function foo($data = ''){
   $this->db->insert('images',$data);
   return $this->db->insert_id();
}
Sign up to request clarification or add additional context in comments.

1 Comment

would you please tell me the purpose of $data = '' in this line function foo($data = '')
0

Hope it will work for you...

$this->db->insert('images',$product_id[0]['product_id']);

2 Comments

How you know the nature and type of array??
@saty I think it always return array.
0

Codeigniter allows you to specify an array of values as the data to insert, but I think your issue is that you're inserting an array of arrays.

If that's the case then use the batch function...

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.