Problem To Save Multiple Row's Data


I have tow model Customer and Order.

Customer table’s fields are c_id, name, phone, email, added_by, added_date


Order table’s fields are o_id, c_id, product_title, price

I want to collect both data from a single form. a customer can order multiple product at a time.

I user jqrelCopy for create order row its fine but Order’s data did not inserted properly just last row’s data was inserted i tried it a lot of time.

Please help me…

I haven’t tried jqrelCopy yet but you’re problem with your code only saving the last row might be because your order row field elements are not in an array format, so newly added rows would just be overwriting previous row elements with the same name. You could try updating your order form element names to be multidimensional arrays with a unique key per row then loop through them when you start inserting data to your database. Your form elements could look something like this:

// Order Row 1

<input type="text" name="Order[0][product_title][]" />

<input type="text" name="Order[0][price][]" />

// Order Row 2

<input type="text" name="Order[1][product_title][]" />

<input type="text" name="Order[1][price][]" />

And your controller code:

$customer = new Customer;

$orders = array();

if (isset($_POST['Customer']))


	$valid = true;

	$customer->attributes = $_POST['Customer'];

	$valid = $customer->validate() && $valid;

	if (!empty($_POST['Order']))


		// Validate each order row and store in the $orders array

		foreach ($_POST['Order'] as $key => $orderPost)


			$order = new Order;

			$order->attributes = $orderPost;

			$valid = $order->validate() && $valid;

			$orders[$key] = $order;


		if ($valid)


			if ($customer->save(false))


				// Loop through all orders and save each one

				foreach ($orders as $order)








There might be an error or 2 in there, just something I wrote done real quick but hopefully you get the idea.

Thank you for you reply. it is working fine. Thanks again… :)