• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mysql insert not working properly

#11
[eluser]CroNiX[/eluser]
Code:
//Single insert using single array
$data = array(
    'product_column' => $product,
    'cat_id_column'  => $cat_id
);

$this->db->insert('table_name', $data);
Code:
//Multiple insert using an array of arrays
$data = array(
  array(
    'product_column' => $product2,
    'cat_id_column'  => $cat_id2
  ),
  array(
    'product_column' => $product,
    'cat_id_column'  => $cat_id
  ),
  array(
    'product_column' => $product3,
    'cat_id_column'  => $cat_id3
  )
);

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

#12
[eluser]ppwalks[/eluser]
Tried this way already:
Code:
$data = array(
        array(
         'product_id' => $product,
          'cat_id'  => $cat_id
         )
  );

Which Gives:

Message: Array to string conversion

Unknown column 'Array' in 'field list'

INSERT INTO `product_category` (`cat_id`, `product_id`) VALUES (Array,Array)

So as you can see it the reverse of the problem I had earlier, this way doesn't work either

#13
[eluser]ppwalks[/eluser]
$product and $cat_id are both arrays

#14
[eluser]ppwalks[/eluser]
Here is the output array

Array ( [0] => Array ( [product_id] => Array ( [0] => 311 [1] => 311 [2] => 311 ) [cat_id] => Array ( [0] => 1 [1] => 4 [2] => 6 ) ) )

So I am absolutely stumped on this one to be honest...

#15
[eluser]CroNiX[/eluser]
Well, you obviously need to format your data correctly. The insert VALUES can't be arrays. The ways I showed are correct, as long as your data is correct.

#16
[eluser]vitoco[/eluser]
So you need to convert two arrays with ids into one with this format :
Code:
$data = array(
  array(
    'product_column' => $product2,
    'cat_id_column'  => $cat_id2
  ),
  array(
    'product_column' => $product,
    'cat_id_column'  => $cat_id
  ),
  array(
    'product_column' => $product3,
    'cat_id_column'  => $cat_id3
  )
);

I think this may help ( only if the indexes are coincident between the two arrays )

Code:
$data = array();

foreach( $product as $index => $value )
{
    $data[] = array(
        'prduct_id'    => $value ,
        'category_id'  => $cat_id[ $index ]
    );
}

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

Slds

#17
[eluser]ppwalks[/eluser]
vitoco you have saved my day and my hair, I have been tearing my hair out staring at the problem all day, so thank you ever so much.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.