Jump to content

Inserting [possible] multiple table rows from one to another table


chet139

Recommended Posts

Hi All,

 

I have the following code which does what I want it to do: To take specific rows from table "cart" and put into table called "orderline":

                //Get all items from cart for that order and put into variables. 	
	$sql2 = "select * from furniture.cart where ordId = $orderId";


	$result2 = @mysql_query($sql2);//runs query
	while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
	{
		$cproductId = $row['productId'];
		$cordId = $row['ordId'];
		$cqty = $row['qty'];

		//take variable values and put into another table called orderline
		$sql3="INSERT INTO furniture.orderline (ordId, productId, ordQuantity) 
		VALUES ($cordId, $cproductId, $cqty)";
		if (!mysql_query($sql3,$con))
		{
			die('Error: ' . mysql_error());
		}
		echo $sql3;	

	}

 

ECHOING the query returns this

INSERT INTO furniture.orderline (ordId, productId, ordQuantity) VALUES (59, 1, 1)INSERT INTO furniture.orderline (ordId, productId, ordQuantity) VALUES (59, 2, 1)

- Which is the SQL statement I expected.... however in addition to this I recieve the following error:

 

Error: Duplicate entry '59-2' for key 1

. But the records that need to be added are added. So with this in mind I removed

if (!mysql_query($sql3,$con))
{
die('Error: ' . mysql_error());
}

 

from the code above. But when I do this only (59, 2, 1)are inserted and not (59, 1, 1).

 

I thought by removing the small if clause would solve my problems as it done what I required.....

 

 

If this is not clear please ask...I am quite new to this

 

Thanks

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.