Please forgive my n00biness, as I am probably doing this a completely moronic way, but this is a way that I have come up with/feel comfortable with and need to get to work. I have correctly pulled my items from a database into a form where the user can add selected items into a shopping cart. The shopping cart being a table called "OrderItems". The user adds the items to the cart via a submit button. The action adds the item to the cart and redirects them back to the catalog page. The catalog page listed below shows two mysql queries, one for the shopping cart, and one for the catalog products. Everything seems to be working correctly except for the fact that when I add a product/item to the cart, it is always adding the same item, regardless of the selection. It is correctly pulling the different items from the database, but the submit form always inserts the same product. Please help.
Here is my code for the catalog page.
<?php
session_start();
session_register ("orderID");
$orderID = session_id();
?>
[COLOR="red"]// Shopping Cart Code[/COLOR]
<?php
echo "<form id='form1' name='form1' method='post' action='checkout.php'>";
[COLOR="Red"]CONNECTION CODE WORKING[/COLOR]
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE orderID = '$orderID'";
$result=mysql_query($sql);
// Define $color=1
$color="1";
echo '<table width="300" border="0" align="left" cellpadding="2" cellspacing="0">';
while($rows=mysql_fetch_array($result)){
// If $color==1 table row color = #FFFFFF
if($color==1){
echo "<tr bgcolor='#FFFFFF'>
<td>".$rows['productName']."<td>".$rows['productPrice']."</td><td><input type='text' size='2' maxlength='4' value='".$rows['productQuantity']."' /></td>
</tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='E1E1E1'>
<td>".$rows['productName']."</td><td>".$rows['productPrice']."</td><td><input type='text' size='2' maxlength='4' value='".$rows['productQuantity']."' /></td>
</tr>";
// Set $color back to 1
$color="1";
}
}
echo "<tr>
<td></td>
<td><input name='submit' type='submit' value='Continue' /><input name='reset' type='reset' value='Clear' /></td></tr>";
echo '</table>';
echo "</form>";
mysql_close();
?>
<?php
[COLOR="red"]// CODE FOR CATALOG PRODUCTS[/COLOR]
[COLOR="red"]CONNECTION SETTINGS WORKING PROPERLY[/COLOR]
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE folderID = $folder";
$result=mysql_query($sql);
// Define $color=1
$color="1";
echo '<table width="500" border="0" align="right" cellpadding="2" cellspacing="0">';
while($rows=mysql_fetch_array($result)){
$productID = $rows['productID'];
$productName = $rows['productName'];
$productPrice = $rows['productPrice'];
// If $color==1 table row color = #FFFFFF
if($color==1){
echo "<tr bgcolor='#FFFFFF'>
<td>
<form action='addItem.php' method='post'>
<input name='orderID' type='hidden' value='".$orderID."' /></td>
<td>".$rows['productName']."</td>
<td>
<input name='productID' type='hidden' value='".$productID."' />
<input name='productName' type='hidden' value='".$productName."' />
<input name='productPrice' type='hidden' value='".$productPrice."' /></td>
<td><input name='addItem' type='submit' value='Add to Cart' /></td>
</tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='E1E1E1'>
<td>
<form action='addItem.php' method='post'>
<input name='orderID' type='hidden' value='".$orderID."' /></td>
<td>".$rows['productName']."</td>
<td>
<input name='productID' type='hidden' value='".$productID."' />
<input name='productName' type='hidden' value='".$productName."' />
<input name='productPrice' type='hidden' value='".$productPrice."' /></td>
<td>
<input name='addItem' type='submit' value='Add to Cart' /></td>
</tr>";
// Set $color back to 1
$color="1";
}
}
echo "<tr>
<td></td>
<td></td>
<td><input name='submit' type='submit' value='Continue' /><input name='reset' type='reset' value='Clear' /></td></tr>";
echo '</table>';
echo "</form>";
mysql_close();
?>
Here is my code for the addItem.php page. Probably not the issue, but here it is incase you need it.
<?php
session_start();
session_register ("orderID");
$orderID = session_id();
?>
<?php
[COLOR="red"]//CONNECTION WORKING PROPERLY[/COLOR]
$sql="INSERT INTO OrderItems (orderID, productID, productName, productPrice)
VALUES
('$_POST[orderID]','$_POST[productID]','$_POST[productName]','$_POST[productPrice]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<strong>Product Added</strong>";
mysql_close($con)
?>
<html>
<head>
<meta http-equiv="refresh" content="1;url=http://www.clientmill.com/smsmmp/step2.php"></head>
<body></body>
</html>