Welcome Guest, Not a member yet? Register   Sign In
How to get Insert_batch to upload my arrays when using a Foreach ?
#1

[eluser]vincej[/eluser]
Hi - I have a form which features multiple products with their id's and their prices etc etc. Therefore I have multiple POST arrays coming into my model - They are not single POST values. OK, I want them uploaded into my DB, so, I thought of using a FOREACH and the Insert_batch function to get them uploaded.

my problem is that insert_batch does not upload the values. However, I have experimented with single POST arrays for example $_POST['name'], using the single $this->db->insert and it works. I guess I could create a nest of FOREACH loops, once for each array - but that very messy. Can anyone see what I am doping wrong with this function - why won't it upload my POST arrays - is it possibly exactly because each POST is an array and not a single value.

If that is the case - is there a neater way of doing it without a nest of FOREACH loops ?

Many Thanks !

Here is the query with the broken INSERT_BATCH:

Code:
function confirmed_Order(){

  $prodid = $_POST['prodid'];
  $customerid = $_SESSION['customerid'];

  $data = array(
  'customerid' =>$customerid,
  'quantity' => $_POST['quantity'],
  'Prodname' => $_POST['name'],
  'prodid' => $_POST['prodid'],
  'pricelb' => $_POST['pricelb'],
  'ordervalue'=>$_POST['ordervalue']
  );

  foreach($prodid as $p) {
  $this->db->insert_batch('confirmedorder', $data); //
  }
}



#2

[eluser]pbflash[/eluser]
insert_batch expects an array or object containing multiple data sets to be inserted into the db. You're only passing an array with a single data set. See the user guide for an example on how to use insert_batch.

#3

[eluser]vincej[/eluser]
Thanks Pbflash - I have seen the guide, I was hoping that because each of the $_POST's is actually an array,the foreach would loop through each POST thus giving insert_batch it's multiple data set. I'm not sure how writing out the array multiple times would actually help exactly because each data set would be the same as the previous one.

Have I missed something ?




Theme © iAndrew 2016 - Forum software by © MyBB