Jump to content

How do I insert into my DB a POST array ?


vincej

Recommended Posts

Hi - I have an HTML form which is dynamically produced using CodeIgniter / PHP and presents products a customer has ordered.  When the customer has finished changing his quantities they must now submit the form.  My problem is that my form will contain many of the same named fields for example, 'Product ID', or 'Product Name' or 'quantity'. So now when it gets processed a typical post array like product id could look like this:

 

Array ( [0] => 28 [1] => 24 [2] => 101 [3] => 23 [4] => 17 )

 

Of course I get an error:

 

Severity: Notice

 

Message: Array to string conversion

 

Filename: mysql/mysql_driver.php

 

I really need some advice on how I should pre-process the various POST arrays so I can get them into my DB.

 

A very grateful student of PHP !

 

Thanks

Link to comment
Share on other sites

Maybe if you show what is really being posted by adding the following to your page and also posting the mysql query you're attempting might help us help you with this situation.  At this point we'd only be guessing.

echo "<pre>";
print_r($_POST);
echo "</pre>";

Link to comment
Share on other sites

Hi - THANK YOU very for helping out ! I'm still learning PHP and this has got me beat. Ok, Here is the data you are asking for. The web form generating the POST variables create arrays as there are many values with the same name.  For example there are lots of Product Id's, lots of Product Names etc etc.

 

So, here is how the POST arrays are created ( CodeIgniter /PHP ):

 

        echo form_hidden('prodid[]', $value['prodid']);
echo form_hidden('name[]', $value['name']);
echo form_hidden('orderid[]', $value['orderid']);
echo form_hidden('pricelb[]', $value['pricelb']);

 

 

So, for example,

print_r ($_POST['prodid']);

produces :

 

Array
(
    [0] => 17
    [1] => 101
    [2] => 27
)

 

or,

 print_r ($_POST['name']);

produces:

 

Array
(
    [0] =>  Ribs Large pack 20 pieces
    [1] => breasts
    [2] => Sirloin Steak 
)

 

The keys line up between the Arrays.

 

The Query is generated with Codeigniter / PHP, which is very obvious. The DB table is called 'confirmedorder'

$data = array(
	'customerid' =>$customerid,
	'quantity' => $_POST['quantity'],
	'name' => $_POST['name'],
	'prodid' => $_POST['prodid'], 
	'pricelb' => $_POST['pricelb'],
	'ordervalue'=>$_POST['ordervalue']
	);
	  
	  $this->db->insert('confirmedorder', $data); 

Link to comment
Share on other sites

Thank you for your help - yes, the Batch method could be the right way to go once I get the variables out of the arrays .  So far I am still experimenting. I found a possible solution on Sitepoint where it uses nested foreach statements where you place an INSERT query for each POST variable.

 

It possibly might work - but it s very, very messy and will produce a rats nest of Foreach statements.  I would prefer something more elegant, but I am not experienced enough to think of it.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.