How to insert arrays into a DB using insert or insert batch |
[eluser]vincej[/eluser]
Hi - I have a web form which produces arrays in the manner of $_POST variables. Example: Code: print_r ($_POST['name']); 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(){ I have also tried : Code: function confirmed_Order(){
[eluser]Samus[/eluser]
Code: function confirmed_Order(){
[eluser]InsiteFX[/eluser]
Code: function confirmed_Order() Is your name an array or are they all seperate items?
[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 !
[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)
[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.
[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( please stick with me on this - I'm really struggling ! :o)
[eluser]InsiteFX[/eluser]
Code: function confirmed_Order() PHP serialize: serialize PHP unserialize: unserialize
[eluser]vincej[/eluser]
I really appreciate your help - however, I still don't get it ... I'm googling serialize array
[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( |
Welcome Guest, Not a member yet? Register Sign In |