update table
Posted: Wed Jan 19, 2011 12:25 am
i have a shopping cart and i cant figure out how to send the final cart information into a table when the user presses checkout, I also need the cart to clear. This is the cart code i need to pass product name, price, quantity and cart total into same fields in a table upon a submit button. I already tried passing the variables using hidden fields in a form I am really new to PHP the table name is customers and i am using an item_id as PK and auto increment.
thanks in advance.
Code: Select all
<?php
$priceTotal="";
$product_name="";
$price="";
$details="";
$cartOutput="";
$cartTotal="";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
$cartOutput="<h2 align='center'>your cart is empty</h2>";
}else{
//index starts at 0
$i=0;
foreach($_SESSION["cart_array"]as $each_item){
$item_id=$each_item['item_id'];
$sql=mysql_query("SELECT * FROM products WHERE id='$item_id'LIMIT 1");
while($row=mysql_fetch_array($sql)){
$product_name=$row["product_name"];
$price=$row["price"];
$details=$row["details"];
}
$pricetotal=$price * $each_item['quantity'];
$cartTotal=$pricetotal + $cartTotal;
//formats currency
setlocale(LC_MONETARY, "en_GB");
$pricetotal=money_format("%!10.2n",$pricetotal);
//Dynamic tabel starts here
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '"></a><br/><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="117" height="178" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>£' . $price . ' </td>';
$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>£' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '"type="submit" value="x"/><input name="item_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .='</tr>';
//items add by 1
$i++;
}
setlocale(LC_MONETARY, "en_GB");
$cartTotal=money_format("%!10.2n",$cartTotal);
$cartTotal="<div align='right'>Cart Total:£" .$cartTotal. " </div>";
}
?>