Hey I've been working about a week on this script but I just can't seem to get it right. I'm trying to build an order form where users are presented with a list (drawn from a mysql relation) of available colors of paint shown in an HTML table. In every <tr> there is one <td> with an text input allowing users to choose their quantity. At the bottom of the form I'd like an add to cart button which stores the items (as well as session_id) into the 'cart' table in my database.
Some of the features I'm hoping for include:
Updating existing quantities if product is already in cart.
If one or more of the posted quantities is higher than the amount in stock, don't add anything to the cart and instead display the page again with an error message detailing which paints exceeded the available stock
If there are no errors display message that items have been added to cart
Some of my main issues include:
I have been doing everything from the same loop and I need to use the loop in areas that are not inside the table.. but as soon as I add another extract() or mysql_fetch_array things go crazy.
I'm not confident at all in what I'm doing
If anybody can help me it would be appreciated.. I've been pulling my hair out over this.
Here is what I've got so far (it's a mess)
PHP Code:
<?php
session_start();
require_once 'lib/database.php';
?>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="include/screen.css">
</head>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table id="colorchart" cellpadding="0" cellspacing="0">
<tr style="background:#CCCCCC;">
<th>No.</th>
<th>Color</th>
<th>Series</th>
<th>Size</th>
<th> </th>
<th>Price</th>
<th>Avail.</th>
<th style="text-align:right; padding-right:25px;">Qty.</th>
</tr>
<?php
$query = "SELECT mc.color_no, mc.color_name, mc.hex, mc.font, mc.series, pd.size, pd.retail, pd.price, pd.qty, pd.item_no
FROM product pd
INNER JOIN mfg_color mc
ON pd.color_id = mc.color_id";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$qtycheck = 0;
for ($i = 0; $i < $rows; ++$i) {
extract(mysql_fetch_array($result));
$postqty = $_POST["qty$i"];
if ($postqty > $qty) {
$red = 'red';
$qtycheck += 1;
$stockerror = "<br /><span class='stockerror'>*Only <span class='qty'>$qty</span> In Stock</span>";
}
elseif ($postqty > 0) {
$red = '';
$stockerror = '';
$sid = session_id();
$query2 = "SELECT c.item_no
FROM cart c
WHERE c.item_no = $item_no AND c.session_id = $sid";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2) == 0) {
$query3 = "INSERT INTO `lashelles`.`cart` (`cart_id`, `item_no`, `cart_qty`, `session_id`, `date`)
VALUES (NULL, '$item_no', '$postqty', '$sid', NOW());";
mysql_query($query3);
}
else {
$query3 = "UPDATE `lashelles`.`cart`
SET cart_qty = cart_qty + $postqty
WHERE item_no = $item_no AND session_id = $sid";
mysql_query($query3);
}
(mysql_query($query3)) ? $postqty = '' : $postqty;
}
else {
$red = '';
$stockerror = '';
}
($postqty == '') ? $value = 0 : $value = $postqty;
($i % 2 == 1) ? $alt = "DDDDDD" : $alt = "FFFFFF";
($font == 'W') ? $font = "FFFFFF" : $font = "000000";
echo <<<_END
<tr style="background:#$alt;">
<td>$color_no</td>
<td style="background:#$hex; color:#$font;">$color_name</td>
<td>$series</td>
<td>$size</td>
<td class="retail">List: $$retail</td>
<td>$$price</td>
<td>$qty</td>
<td style="text-align:right;"><input type="text" size="3" value="$value" name="qty$i" style="border:1px solid $red;"/>$stockerror</td>
</tr>
_END;
}
?>
<tr><td colspan="8" style="border:0; text-align:right;"><input class="addtocart" type="submit" value="ADD TO CART" name="addtocart" /></td></tr>
</table>
</form>