Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem to insert shopping cart data into a database.

fellafella penangMember Posts: 1
edited May 2016 in PHP
<?php require_once("dbcontroller.php"); require_once("session.php"); $db_handle = new DBController(); if(!empty($_GET["action"])) { switch($_GET["action"]) { case "add": if(!empty($_POST["quantity"])) { $productById = $db_handle->runQuery("select* from menu where id='" . $_GET["id"] . "'"); $itemArray = array($productById[0]["id"]=>array('name'=>$productById[0]["name"], 'id'=>$productById[0]["id"], 'quantity'=>$_POST["quantity"], 'price'=>$productById[0]["price"])); if(!empty($_SESSION["cart_item"])) { if(in_array($productById[0]["id"], $_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($productById[0]["id"] == $k)$_SESSION["cart_item"][$k]["quantity"]=$_POST["quantity"]; } } else { $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); } } else { $_SESSION["cart_item"] = $itemArray; } } break; case "remove": if(!empty($_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["id"] == $k) unset($_SESSION["cart_item"][$k]); if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]); } } break; case "empty": unset($_SESSION["cart_item"]); break; } } ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Welcome

.auto-style1 { text-align: center; border: 2px solid #00FFFF; } .auto-style2 { text-align: right; border: 2px solid #00FFFF; } .auto-style3 { vertical-align: middle; } .auto-style4 { color: #00FFFF; font-size: x-large; font-family: "Cooper Black"; } .auto-style5 { text-align: center; border: 0 solid #FFFFFF; } .auto-style6 { text-align: center; } .auto-style8 { text-align: right; } .auto-style9 { font-family: "Cooper Black"; font-size: medium; }

Welcome, <?php echo $user_check; ?>
Shopping Cart Empty Cart
<?php if(isset($_SESSION["cart_item"])){ $item_total = 0; ?> <?php foreach ($_SESSION["cart_item"] as $item){ ?> <?php $item_total += ($item["price"]*$item["quantity"]); } ?>
Name Id Quantity Price Action
<?php echo $item["name"]; ?> <?php echo $item["id"]; ?> <?php echo $item["quantity"]; ?> <?php echo "RM".$item["price"]; ?> " class="btnRemoveAction">Remove Item
Total: <?php echo "RM".$item_total; ?>
<?php } ?>
Coffee Menu
<?php $menu_array = $db_handle->runQuery("select * from menu"); if (!empty($menu_array)) { foreach($menu_array as $key=>$value){ ?>
">
<?php echo $menu_array[$key]["name"]; ?>
<?php echo $menu_array[$key]["Size"]; ?>
<?php echo "RM".$menu_array[$key]["price"]; ?>
<?php } } ?>

This is the example of my code. But I did not know how to write the code for insert data.
How to write the script for inserting the shopping cart data after user click on the Checkout button.

Tagged:
Sign In or Register to comment.