Inserting Order 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
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Inserting Order Problem.

Post by mmc01ms »

HI guys, I got a slight problem with one of my scripts. What I want to do is for each item that is in the cart I would like to be able to check each individual items stock_level. Now if that stock_level is below a certain threshold then it will have different data put into the database i.e preorder, processing, awaiting stock etc. The problem is that I can get the information about stock level I want however if I enter two items or more it gives me both values of the stock level for eg. 0 for vinyl A and 2 for vinyl B. This means that I can’t check both in isolation as they spit our the stock_level for both. This is because im using a function to get the details of the vinyls in the cart. Is there anyway I can get these and check them one by one after the get_vinyls_function using an array or something? Any actually code tips would be much appreciated or a point in the right direction.


Code is

Code: Select all

function insert_order($order_details)
		{
			//function inserts the order details into the order table.
			
			
			extract($order_details);
			
			
			if(!$delivery&&!$name&&!$title&&!$surname&&!$address1&&!$town&&!$town&&!$city&&!$county&&!$postcode&&!$total&&!$customer_id&&!$number&&!$expiry&&!$start&&!$issue&&!$type&&!$cardname)
			{
				
				$delivery = $_SESSIONї'shipping'];
				$name = $ship_name;
				$title = $ship_title;
				$surname = $ship_surname;
				$address1 = $ship_address_1;
				$town = $ship_town;
				$city = $ship_city;
				$county = $ship_county;
				$postcode = $ship_postcode;
				$total = ($_SESSIONї'total_price']+$_SESSIONї'shipping']);
				$customer_id = $_SESSIONї'customer_id'];
				
				$number = $card_number;
				$expiry = $expiry_date;
				$start = $start_date;
				$issue = $issue_number;
				$type = $card_type;
				$cardname = $card_name;
				
			}
			//-----------------------------------Insert Order into Database---------------------------------------------
			
			
			$link_id = db_connect();
			$date = date('Y-m-d-H-i-s');//get current date
			
			
			//insert order into orders table.
			
			$query = "insert into orders (order_id, customer_id, order_status, date_order_placed, delivery_charge, ship_name, ship_surname,
 						ship_title, ship_address1, ship_address2, ship_city, ship_county, ship_postcode, order_note, total)
						values(NULL, '$customer_id', 'Processing Order', '$date', '$delivery','$name','$surname','$title','$address1',
 						'$town','$city','$county','$postcode', NULL,'$total')";
				
				$result = @mysql_query($query, $link_id) or die(mysql_error());
			
			
			if (!$result)
				return false;
			
			
			//<!-----------------------------------Get Order_id for current order--------------------------------------------->
			
			
			//now we want to put each item into the ordered_items table so we must make sure we get the right order and do a query to retirieve the order.
			$query = "select order_id from orders where customer_id = '$customer_id' and date_order_placed = '$date'";
			
			$result = @mysql_query($query) or die(mysql_error());
			var_export($query);
			
			$order_id = mysql_result($result, 0, 'order_id');
			
			
			if(!$result)
				return false;
			
			//<!-----------------------------------Insert Payment Details into Database--------------------------------------------->
			
			//do the query and insert the values
			$query = "insert into payments values('".$_SESSION&#1111;'customer_id']."','$order_id','$total','$number','00-00',
				'$issue','00-00','$type','$cardname')";
				
				
				$result = @mysql_query($query, $link_id) or die(mysql_error());
			
			
			if(!$result)
				return false;
			
			
			//<!-----------------------------------Loop through each item in the ordered items table and insert--------------------------------------------->
			
			foreach($_SESSION&#1111;'cart'] as $catno => $qty)
			&#123;
				
				$detail = get_vinyl_details($catno);
				$query = "delete from ordered_items where order_id = '$order_id' and cat_no = '$catno'";
				$result = mysql_query($query);
				//var_export($detail);
				print $detail&#1111;'stock_level'];
				
				//if($detail&#1111;'stock_level'] < 0);
			//	&#123;
					$query = "insert into ordered_items values ('$catno','$order_id','On Order', '".$detail&#1111;'price']."','$qty')";
					$result = @mysql_query($query, $link_id) or die(mysql_error());
				//&#125;
				
				//if($detail&#1111;'stock_level'] > 0);
				//&#123;
				//	$query = "insert into ordered_items values ('$catno','$order_id','Processing', '".$detail&#1111;'price']."','$qty')";
				//	$result = @mysql_query($query, $link_id) or die(mysql_error());
				//&#125;
				
				if (!$result)
					return false;
			&#125;
			
			
			return $order_id;
		&#125;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Inserting Order Problem.

Post by timvw »

mmc01ms wrote:

Code: Select all

function insert_order($order_details)
&#123;
  $delivery = $_SESSION&#1111;'shipping'];
If you are getting data from a session, you might want to start a session first if it hasn't been started before

if (!isset($_SESSION)) session_start();

and test if the shipping key exits...

mmc01ms wrote:

Code: Select all

$date = date('Y-m-d-H-i-s');//get current date
No need to do this, SQL has a function NOW() that gives the current date.

mmc01ms wrote:

Code: Select all

$order_id = mysql_result($result, 0, 'order_id');
Actually, what you need is http://www.php.net/mysql_insert_id
Post Reply