Page 1 of 1

loop problem

Posted: Sun Mar 26, 2006 10:15 am
by aceconcepts
Hi there,

I am using a loop to try and add records of data from two tables to a single table. The problem is that my code only adds one record to the table.

Code: Select all

<?php

include"conn.inc.php";

//SET ALL VARIABLES NEED TO STORE IN ORDER_TABLE--------------------------
$customerid = $_REQUEST['cid'];
$deliveryid = $_REQUEST['del'];
$sessid = $_REQUEST['sess'];
$today = date("d-m-Y");
//------------------------------------------------------------------------

//STORE ORDER AND GET ORDER NUMBER FROM ID
$insert_details = "INSERT INTO product_order (
			order_date, customer_id)
			VALUES (
			'$today',
			'$customerid')";
$insert_order = mysql_query($insert_details)
	or (mysql_error());
//SET ORDER NUMBER
$orderid = mysql_insert_id();

//GET ORDER DETAILS FOR EACH ITEM ORDERED (order_qty and product_id)
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query)
	or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
extract($row);
$order_qty = $row['carttemp_quan'];
$productid = $row['carttemp_prodnum'];

//ADD EACH ITEM TO ORDER DETAILS TABLE------------------------------------
$insert4 = "INSERT INTO order_details (
			order_num, order_date, order_qty, customer_id, product_id, del_id)
			VALUES (
			'$orderid',
			'$today',
			'$order_qty',
			'$customerid',
			'$productid',
			'$deliveryid')";
$insert_details4 = mysql_query($insert4)
	or (mysql_error());
}
?>
What am i doing wrong?

Posted: Sun Mar 26, 2006 10:28 am
by AlecH
I would take out:

Code: Select all

extract($row);
I think that might be causing the problem, othwerwise I dont see anything else.

Posted: Sun Mar 26, 2006 10:30 am
by aceconcepts
ill give it a go

Posted: Sun Mar 26, 2006 10:34 am
by aceconcepts
still doesn't work.

It seems as though the loop is only looping once because only one record from the shopping cart table is inserted into the order table.

Posted: Sun Mar 26, 2006 10:42 am
by John Cartwright
pretty busy just skimming posts, but try changing mysql_fetch_array to mysql_fetch_assoc, or mysql_fetch_array($result, MYSQL_ASSOC)

Posted: Sun Mar 26, 2006 4:52 pm
by s.dot
change

$insert_details4 = mysql_query($insert4)
or (mysql_error());

to

$insert_details4 = mysql_query($insert4)
or die(mysql_error());

Posted: Sun Mar 26, 2006 4:55 pm
by s.dot
although I doubt that would solve your problem.

Is there more than one record to be retrieved?
Do each of the records have a unique fieldname? (such as a primary key)