Jump to content



Recommended Posts

Hi i am trying to pass values from a cart to a database using foreach.


This is my code:

$columns = array('order_id', 'order_item_id', 'order_product_name', 'order_product_dimensions', 'order_product_options', 'order_product_quantity', 'order_product_price', 'order_product_subtotal', 'order_product_vat', 'order_product_delivery','order_product_total','order_product_sku','order_product_image_name','order_product_image_type','order_product_image_path','order_product_image_size','order_product_artwork_link','order_product_artwork_name','order_product_artwork_type','order_product_artwork_path','order_product_artwork_size','order_firstname','order_lastname','order_companyname','order_email','order_tel','order_billing_address','order_billing_address2','order_billing_town','order_billing_county','order_billing_postcode','order_shipping_address','order_shipping_address2','order_shipping_town','order_shipping_county','order_shipping_postcode','order_status','order_username');
$data = array_fill_keys($columns, 'NULL');

foreach($data as $key => $value) {

	$data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_escape_string($_POST[$key])."'";
	$query = "INSERT INTO orders (id, order_created_at, ".implode(", ",$columns).")
VALUES (null, NOW(), ".implode(",",$data).')';



Problem is posting 2 or more products into the database.


How do i split the values so it enters each product into the database?

Link to comment
Share on other sites

You would need to separate each set of values with a comma, and then you will insert multiple rows. Also, take the actual INSERT query out of the loop since you only want that to happen once. Your query should look like

INSERT INTO orders (column1, column2, column2) VALUES (1,2,3), (4,5,6), (7,8,9)

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.

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.