Jump to content

Recommended Posts

Hi Guys,

 

I am trying to write to two tables. One is orders and one is productorder.

OrderID in the orders table is auto increment and is a foreign key in the productorder table. When an order is saved it writes it first to the orders table and then in the same action writes it to the productorder table but I need to get the orderID value from the first query to be entered into the productorder table in the second query.

 

I Hope this makes sense.

 

Here is my code, at the moment I am hard coding the OrderID in the productorder table as I dont know how to do it properly.

 

<?php
require "connect.php";
$empID = $_SESSION['empID'];
$username = $_SESSION['username'];

if($_SESSION['basket'] == !null)
{
	$query =  "insert into orders values ('','".$empID."','".$username."','".date("d-M-Y")."')";
	$result = @mysql_query($query, $connection) 
	or die ("Unable to perform query<br>$query");

	foreach($_SESSION['basket'] as $key => $product)
	{
		$query2 = "insert into productorder values ('[color=yellow]1[/color]','".$_SESSION['basket'][$key]['URN']."','".$_SESSION['basket'][$key]['quantity']."','Awaiting Approval')";
		$result2 = @mysql_query($query2, $connection) 
		or die ("Unable to perform query<br>$query2");
	}
	unset($_SESSION['basket']);
	$message2 = "Order has successfully been submitted";
	header("Location: StationaryMain.php?var=URN");
	exit();
}
else
{
	$message1 = "There are no products in the Stationary Basket";
	header("Location: StationaryBasket.php?message1=$message1");
	exit();
}

?>

Here's the revised code. BTW: You do not need to assign the query to a variable if you are not going to use that variable (e.g. for an insert). Also, have you tested the ELSE caluse? You may need to URL encode that message to work since it has spaces in it. Your success path has a message with spaces, but you don't use it.

 

<?php
require "connect.php";
$empID = $_SESSION['empID'];
$username = $_SESSION['username'];

if($_SESSION['basket'] == !null)
{
	$query =  "insert into orders values ('','".$empID."','".$username."','".date("d-M-Y")."')";
	@mysql_query($query, $connection) or die ("Unable to perform query<br>$query");
	$order_id = mysql_insert_id();

	foreach($_SESSION['basket'] as $key => $product)
	{
		$query2 = "insert into productorder values ($order_id,'".$_SESSION['basket'][$key]['URN']."','".$_SESSION['basket'][$key]['quantity']."','Awaiting Approval')";
		@mysql_query($query2, $connection) or die ("Unable to perform query<br>$query2");
	}
	unset($_SESSION['basket']);
	$message2 = "Order has successfully been submitted";
	header("Location: StationaryMain.php?var=URN");
	exit();
}
else
{
	$message1 = "There are no products in the Stationary Basket";
	header("Location: StationaryBasket.php?message1=$message1");
	exit();
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.