Welcome Guest, Not a member yet? Register   Sign In
How to insert arrays into a DB using insert or insert batch
#1

[eluser]vincej[/eluser]
Hi - I have a web form which produces arrays in the manner of $_POST variables. Example:

Code:
print_r ($_POST['name']);

// PRODUCES:
Array
(
    [0] => 25 Piece Bag
    [1] => Ribs Small Pack 5 Pieces
    [2] => breasts
    [3] =>  Whole Chicken:  6 - 8 lbs
    [4] =>  Ribs Large pack 20 pieces
)


There are many of these for product price, product ID etc. etc.

I want to upload these to my DB, so I have the following model. It is talking to to the DB as I can see the primary key auto increment, but I am not getting any data in. I have checked the spellings and all is well. I have also tried using the simple INSERT with a string, rather than INSERT_BATCH with an array, and I get the same outcome.

I must be doing something wrong. If anyone can offer me advice I am very Grateful !

Model:

Code:
function confirmed_Order(){
          
  $quantity =  $_POST['quantity'];
  $name = $_POST['name'];
  $prodid = $_POST['prodid'];
  $pricelb = $_POST['pricelb'];
      
          foreach($name as $n) {
   $data = array ('Prodname' => $name);
   $this->db->insert_batch('confirmedorder', $data);  
     }
    }


I have also tried :

Code:
function confirmed_Order(){
          
  $quantity =  $_POST['quantity'];
  $name = $_POST['name'];
  $prodid = $_POST['prodid'];
  $pricelb = $_POST['pricelb'];
      
          foreach($name as $n) {
   $data = array ('Prodname' => $n);
   $this->db->insert('confirmedorder', $data);  
     }
    }

#2

[eluser]Samus[/eluser]
Code:
function confirmed_Order(){
          
  $quantity =  $_POST['quantity'];
  $name = $_POST['name'];
  $prodid = $_POST['prodid'];
  $pricelb = $_POST['pricelb'];
      
   $data = array ('Prodname' => $name);
   $this->db->insert('confirmedorder', $data);
    }
???
#3

[eluser]InsiteFX[/eluser]
Code:
function confirmed_Order()
{
    $data = array(          
        'quantity' => $this->input->post('quantity', TRUE),
        'name'     => $this->input->post('name', TRUE),
        'prodid'   => $this->input->post('prodid', TRUE),
        'pricelb'  => $this->input->post('pricelb', TRUE)
    );

   $this->db->insert('confirmedorder', $data);
}

Is your name an array or are they all seperate items?
#4

[eluser]vincej[/eluser]
Hi Insite - I'm not sure I understand what you are asking. In the HTML form 'name' refers to all the product names and is captured as an array, like this:

Code:
echo form_hidden('name[]', $value['name']);

I have since my earlier post realised why insert_batch worn't work: this function requires that the DB column name is the same as the index on your values. well, my values have no meaningful index.

Thanks so much for your help ! I'm struggling with this big time !


#5

[eluser]vincej[/eluser]
Hi Insite - I tried your code ( thank you ! ) Unfortunately it did not work. I got an error message: "Array to string conversion" .. so I tried it using insert_batch and the error message went away, but nothing was written to the DB.

Bummer, I'm really stuck for ideas ... got any good ones ? :o)
#6

[eluser]InsiteFX[/eluser]
To place an array into a database field it needs to be serialized!

You can take a look at the CodeIgniter Session Class and see how they serialize and un-serialize the data.
#7

[eluser]vincej[/eluser]
Thanks Insite - sorry for being a Newb on this, I read the paragraph on how a cookie is a serialized array. Are you suggesting that I place my $_POST['name'] or a derived variable ie $name as an index inside CI Session and then use that to do a INSERT _BATCH ?? That is to say something like this:

Code:
$newdata = array(
                   'name'  => $name,
                   'prodid'=> $prodid,
                   'quantity' => $quantity
               );

$this->session->set_userdata($newdata);

$this->db->insert_batch('confirmedorder', $newdata);

please stick with me on this - I'm really struggling ! :o)
#8

[eluser]InsiteFX[/eluser]
Code:
function confirmed_Order()
{
    $data = array(          
        'quantity' => $this->input->post('quantity', TRUE),
        'name'     => serialize($this->input->post('name', TRUE)),
        'prodid'   => $this->input->post('prodid', TRUE),
        'pricelb'  => $this->input->post('pricelb', TRUE)
    );

   $this->db->insert('confirmedorder', $data);
}

// to unserialize it
$name = array();

// this would be the field from your db table.
$name = unserialize($field_name);

PHP serialize:
serialize

PHP unserialize:
unserialize
#9

[eluser]vincej[/eluser]
I really appreciate your help - however, I still don't get it ... I'm googling serialize array
#10

[eluser]vincej[/eluser]
Ok Insite, I've googled it ( saw your links ) and I get it. I'm not getting an error and the DB is incrementing but I still get no values being written ... perhaps a foreach loop somewhere ?



Code:
$data = array(          
        'quantity' => serialize($this->input->post('quantity', TRUE)),
        'name'     => serialize($this->input->post('name', TRUE)),
        'prodid'   => serialize($this->input->post('prodid', TRUE)),
        'pricelb'  => serialize($this->input->post('pricelb', TRUE))
    );


   $this->db->insert('confirmedorder', $data);




Theme © iAndrew 2016 - Forum software by © MyBB