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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.