Page 1 of 1

[SOLVED] While... function doesn't seem to be working

Posted: Fri Jul 02, 2004 2:16 pm
by dardsemail
Hi,

I have a script (listed below) which is intended to enter items into a database based on the results of a query. I need it to enter several rows of data based on what has been included in a shopping cart. In any event, I have tried to use a 'while' function, but am only getting 1 row returned. However, there are 5 lines of data in the source table... Help!

Code: Select all

<?php
	//Retrieve the item details of the cart 
	$orderDetailQuery="SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'";
	$result = mysql_query ($orderDetailQuery) or die("ERROR, ERROR!".mysql_error()); 
	$numRows = mysql_num_rows($result);
	echo $numRows;
	
	//Go through each of the items in the cart
	while ($row=@mysql_fetch_array($result))
	{	
		$piecenum = $row['piecenum'];
		$price = $row['price'];
		$description = $row['description'];
		$qty = $row['qty'];
		$randomOrderID= $_SESSION['randomOrderID'];
		$insertquery="INSERT INTO orderdetails(randomOrderID, itemNum, itemDesc, quantity, price) VALUES('$randomOrderID','$piecenum','$description','$qty', '$price')";
		$result = mysql_query ($insertquery) or die("ERROR, ERROR!".mysql_error());
	}
?>
What am I doing wrong?

Posted: Fri Jul 02, 2004 2:24 pm
by pickle
In your for loop, you're result for your insert query is also named $result, which is what you're looping on. Rename your inner $result to something else, and it should work.

Posted: Fri Jul 02, 2004 2:37 pm
by dardsemail
Amen! Thanks so much! DONE!