Ok after playing around with this suggestion and numerous others I have a solution that should (in theory) work.
So I have a page listing all the products, next to each item I now have one quantity box, one size box and a add to cart button.
When a customer clicks the add to cart button the form has the ProductID in its name, so when the page refreshes if ProductID > 0 then it carries out the following:
1)
First it checks if they have already clicked add to cart before by checking if a OrderID has been assigned, if its zero I create a new record with 0 as a CustomerID for now, then get the newly assigned OrderID.
2) Then I insert the order information (quantity, size) into the OrderItem table, just ignore the total, subtotal as that isn't done yet.
Here is my code for the insert part, if i comment it out the page loads, and if I comment it out and echo ProductID above it, its always nothing even when the 'Add to Cart' button has been clicked.
PHP Code:
<?PHP
session_start();
//connect to DB
require 'inc/connect.inc.php';
//check if add to cart has been pressed
if ($_REQUEST['ProductID'] == 0)
{
// check if order exists
if ($_SESSION['OrderID'] >0) {
// check to make sure there is a DB record
$sqlcount="SELECT count(*) thecount FROM Orders WHERE OrderID = $_SESSION['OrderID']";
$resultcount=mysql_query($sqlcount);
$row=mysql_fetch_array();
if ($row[0]==0) {
// if not create it - insert into Orders (OrderID, etc) values ();
$sqlcreate="INSERT INTO Orders(CustomerID_FK) values (0)";
$resultcreate=mysql_query($sqlcreate);
// get new OrderID and save in $_SESSION['OrderID']
$_SESSION['OrderID']=mysql_insert_id();
}
} else {
$sqlcreate2="Insert INTO Orders(CustomerID_FK) values (0)";
$resultcreate2=mysql_query($sqlcreate2);
// get new OrderID and save in $_SESSION['OrderID']
$_SESSION['OrderID']=mysql_insert_id();
}
//For new PHP, used to make data safe before sending a query to MySQL,
//prevents SQL Injection into the input fields
//Had to change from PHP5 expressions to suit PHP4 server
$quantity=mysql_escape_string($_POST['quantity']);
$size=mysql_escape_string($_POST['size']);
$price=mysql_escape_string($_POST['price']);
$subtotal=mysql_escape_string($_POST['subtotal']);
$total=mysql_escape_string($_POST['total']);
// add order item
$sqlinsert="Insert INTO OrderItem SET,
OrderID_FK = " . $_SESSION['OrderID'] . ",
ProductID_FK = " . $_REQUEST['ProductID'] . ",
OrderItemQuantity = '$Quantity',
OrderItemSize = '$Size',
OrderItemUnitprice = '$Price',
OrderItemSubTotal = '$Subtotal',
OrderItemTotal = '$Total'";
$resultinsert=mysql_query($sqlinsert);
} //end request productid
?>
Heres an extract from part of my form:
PHP Code:
<form name="<?php echo 'order'.$ProductID; ?>" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name=ProductID value=<?PHP echo $ProductID;?>>
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td class="text2">Quantity</td>
<td><select name="Quantity">
<option value=1 selected>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select ></td>
</tr>
<tr>
<td class="text2">Size</td>
<td><select name="Size">
<option value="A1" selected>A1</option>
<option value="A2">A2</option>
<option value="A3">A3</option>
</select ></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit" value="Add to Cart">
</div></td>
</tr>
</table>
</form>
At the moment, the ProductID isn't working.... being assigned, whatever.
I also want to display a message beside each product saying that have already ordered one/two sizes of the product, with the option to add another like normal - size,quantity boxes.
I will then have a Show Cart page that displays the OrderItem table records for the OrderID used, and delete them if they choose not to login and save the order request.
Im not sure if Ive made this clear or posted enough code, but if anyone understands what im trying to achieve and can point out what I need in my code, why that would just be splendid
Thankyou