loop problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

loop problem

Post 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?
AlecH
Forum Commoner
Posts: 27
Joined: Fri Feb 24, 2006 4:22 pm
Location: New Hampshire

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

ill give it a go
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

change

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

to

$insert_details4 = mysql_query($insert4)
or die(mysql_error());
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply