Bakht
Bakht

Reputation: 113

Get max value from table

I am applying this query:

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

to get max value from table.

But when i add this result to db in another table. like this:

$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

I am using codeigniter.

Upvotes: 0

Views: 117

Answers (3)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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();
}

Upvotes: 1

Carlton
Carlton

Reputation: 5721

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')

Upvotes: 0

Shravan Sharma
Shravan Sharma

Reputation: 989

Hope it will work for you...

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

Upvotes: 0

Related Questions