"Insert Into" statement won't update sql table

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
kennyw1000
Forum Newbie
Posts: 3
Joined: Wed Jun 15, 2005 1:00 pm
Contact:

"Insert Into" statement won't update sql table

Post by kennyw1000 »

I have a php script that gets the information from an order form, and puts the information into 3 different tables. The first table is the "payment" table, the second is the "orders" table, and the third is the "line_item" table. The information is entered into the first and third tables correctly, but no data is entered into the second table. The code is exactly the same for the first and second tables, however the second table, "orders" gets no data inserted into it. I have included the code below, so you can see what I'm doing. The problem is in the //insert order information. I have tried using the code 2 ways, as you will see in the //original insert order information. Neither way writes to the "orders" table in the Database. However, the "payments" table is being entered correctly.

Code: Select all

function process_order()
	{
		// Get order form variables
		$comments = $_REQUEST['comments'];
		$order_total = $_REQUEST['order_total'];
		$card_type = $_REQUEST['card_type'];
		$card_name = $_REQUEST['card_name'];
		$card_number = $_REQUEST['card_number'];
		$card_expiration_date = $_REQUEST['card_expiration_date'];
		$card_ccv_number = $_REQUEST['card_ccv_number'];
		$username = $_SESSION['username'];
		$sql = "SELECT customer_id FROM customer_name WHERE username = '".$username."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$customer_id = $row['customer_id'];
		$order_time = date('Y-m-d h:i a');
		$check_order_time = $order_time;
		$order_string = $_SESSION['lineItems_in_cart'];
		
		if($order_string != '')
		{
			// Connect to DB
			$db = new DATABASE;
			$db->db('connect');
			
			// insert  payment information
			$result = mysql_query("INSERT INTO `payment`
				(customer_id, payment_type, payment_amount, card_type, card_name, card_number, card_expiration, card_ccv_number, date)
				VALUES
				(\"$customer_id\", 'cc', \"$order_total\", \"$card_type\", \"$card_name\", \"$card_number\", 
				\"$card_expiration_date\", \"$card_ccv_number\", \"$order_time\")");
			$payment_id = mysql_insert_id();			
			
                     // Get cart weight
			$total_weight = $this->get_cart_weight();
			
			// insert order information
			$result = mysql_query("INSERT INTO `orders`
				(customer_id, payment_id, order_string, order_total, order_weight, order_status, comments, date)
				VALUES
				(\"$customer_id\", \"$payment_id\", \"$order_string\", \"$order_total\", \"$total_weight\", 'Pending', 
				\"$comments\", \"$order_time\")");
			$order_id = mysql_insert_id();
			
			//original insert order line
			//$sql = "INSERT INTO `orders`
				//(customer_id, payment_id, order_string, order_total, order_weight, order_status, comments, date)
				//VALUES
				//(\"$customer_id\", \"$payment_id\", \"$order_string\", \"$order_total\", \"$total_weight\", 'Pending', 
				//\"$comments\", \"$order_time\")";
			//$result = mysql_query($sql);
			//$order_id = mysql_insert_id();
			
			$cart_lineItems = $this->get_cart();
			$cart_lineItem_count = count($cart_lineItems);
			$i = 0;
			$lineItem_id = 1;
			while ($i < $cart_lineItem_count) 
				{ 
				$lineitems = explode("~", $cart_lineItems[$i]);
				$product_id = $lineitems[0];
				$quantity = $lineitems[1];
				$ship_to = $lineitems[2];
				$sql = "INSERT INTO `line_item`
					(lineitem_id, order_id, product_id, ship_to, customer_id, quantity)
					VALUES
					(\"$lineitem_id\", \"$order_id\", \"$product_id\", \"$ship_to\", \"$customer_id\", 
					\"$quantity\")";
				$result = mysql_query($sql);
				//echo "sql: ".$sql."<br>";
				$lineItem_id++;
				$i++;
				}
				//exit;
			// Unload the cart
			unset($_SESSION['lineItems_in_cart']);
			
                     //print $orderinfo;
                     print "<p CLASS=\"clsP\">Thank you for submitting your order.  Feel free to continue to use the site, just remember to <A HREF='index.php?page=logout' >Logout</A> when you are finished!</p>";
			}
		else
			{
			print "<p CLASS=\"clsP\">There is nothing in your cart to process!</p>";
			}
		}
Data in the third table "line_item" is also being entered correctly.

Anyone have any ideas?

Thanks.

d11wtq | Please use

Code: Select all

tags when posting PHP code [b](AND DON'T DISABLE BBCODE   )[/b][/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Please review the Posting Code in the Forums.

viewtopic.php?t=21171

AND DO NOT DOUBLE POST
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

$result = mysql_query("INSERT INTO `orders`
(customer_id, payment_id, order_string, order_total, order_weight, order_status, comments, date)
VALUES
(\"$customer_id\", \"$payment_id\", \"$order_string\", \"$order_total\", \"$total_weight\", 'Pending',
\"$comments\", \"$order_time\")") or die (mysql_error()); # or die
kennyw1000
Forum Newbie
Posts: 3
Joined: Wed Jun 15, 2005 1:00 pm
Contact:

Post by kennyw1000 »

ol4pr0, Thanks for the post. It worked like a charm. There was a field missing in the table, and that helped me troubleshoot.
Post Reply